c#LoadXml随机返回它们是多个根元素,没有任何明显的原因
我目前正在开发一些基于httpwebrequest的解决方案,这些解决方案通过远程(Prestashop)MYSQL数据库和返回信息进行通信 这个概念工作起来很有魅力,我使用它通过构建Xml响应加载一些对象,比如客户、组或产品,我反序列化了。所有对象都返回相同类型的过程,并且都可以完美地加载。 但就产品而言,我最近遇到了一个神秘的bug,说我的xml包含几个根元素,这是完全错误的。我对Xml的构建感到非常自在,所以我没有找到出路就发疯了。 操作反序列化的c#函数非常简单:c#LoadXml随机返回它们是多个根元素,没有任何明显的原因,c#,xml,load,root,C#,Xml,Load,Root,我目前正在开发一些基于httpwebrequest的解决方案,这些解决方案通过远程(Prestashop)MYSQL数据库和返回信息进行通信 这个概念工作起来很有魅力,我使用它通过构建Xml响应加载一些对象,比如客户、组或产品,我反序列化了。所有对象都返回相同类型的过程,并且都可以完美地加载。 但就产品而言,我最近遇到了一个神秘的bug,说我的xml包含几个根元素,这是完全错误的。我对Xml的构建感到非常自在,所以我没有找到出路就发疯了。 操作反序列化的c#函数非常简单: public stat
public static Dictionary<string, string> get(IRestResponse response)
{
var objectToLoad = new Dictionary<string, string>();
XmlDocument doc = new XmlDocument();
doc.LoadXml(response.Content.ToString());
XmlNodeList idNodes = doc.SelectNodes("object");
foreach (XmlNode node1 in idNodes)
{
foreach (XmlNode node in node1.ChildNodes)
{
objectToLoad.Add(node.Name, node.InnerText);
}
}
return objectToLoad;
}
publicstaticdictionary获取(IREStreponse响应)
{
var objectToLoad=new Dictionary();
XmlDocument doc=新的XmlDocument();
doc.LoadXml(response.Content.ToString());
XmlNodeList idNodes=doc.SelectNodes(“对象”);
foreach(idNodes中的XMLNode1节点)
{
foreach(node1.ChildNodes中的XmlNode节点)
{
objectToLoad.Add(node.Name,node.InnerText);
}
}
返回objectToLoad;
}
为了说明这一点,下面是加载一个工作完美的组的第一个示例:
<?xml version="1.0" encoding="UTF-8"?>
<object>
<id>4</id>
<reduction>0</reduction>
<price_display_method>0</price_display_method>
<show_prices>1</show_prices>
<date_add>2014-09-23 16:23:05</date_add>
<date_upd>2014-10-14 09:27:09</date_upd>
<name>Public VIP</name>
<id_lang>1</id_lang>
</object>
4.
0
0
1.
2014-09-23 16:23:05
2014-10-14 09:27:09
公共贵宾
1.
但当我加载对象类型产品时:
<?xml version="1.0" encoding="UTF-8"?>
<object>
<id>6</id>
<id_shop_default>1</id_shop_default>
<id_manufacturer>1</id_manufacturer>
<id_supplier>1</id_supplier>
<reference>MTDENIMJR</reference>
<supplier_reference></supplier_reference>
<location></location>
<width>0</width>
<height>0</height>
<depth>0</depth>
<weight>0.05</weight>
<quantity_discount>0</quantity_discount>
<ean13>815264500995</ean13>
<upc>815264500995</upc>
<cache_is_pack>0</cache_is_pack>
<cache_has_attachments>0</cache_has_attachments>
<is_virtual>0</is_virtual>
<id_category_default>9</id_category_default>
<id_tax_rules_group>1</id_tax_rules_group>
<on_sale>1</on_sale>
<online_only>0</online_only>
<ecotax>0</ecotax>
<minimal_quantity>1</minimal_quantity>
<price>10.313</price>
<wholesale_price>2.9</wholesale_price>
<unity></unity>
<unit_price_ratio>0</unit_price_ratio>
<additional_shipping_cost>0</additional_shipping_cost>
<customizable>0</customizable>
<text_fields>0</text_fields>
<uploadable_files>0</uploadable_files>
<active>1</active>
<redirect_type>404</redirect_type>
<id_product_redirected>0</id_product_redirected>
<available_for_order>1</available_for_order>
<available_date>0000-00-00</available_date>
<condition>new</condition>
<show_price>1</show_price>
<indexed>1</indexed>
<visibility>both</visibility>
<cache_default_attribute>0</cache_default_attribute>
<advanced_stock_management>0</advanced_stock_management>
<date_add>2013-02-27 08:03:35</date_add>
<date_upd>2018-05-09 10:59:40</date_upd>
<pack_stock_type>3</pack_stock_type>
<groups_allowed></groups_allowed>
<flashsale>0</flashsale>
<id_google_category>36</id_google_category>
<meta_description>Vernis à Ongle Morgan Taylor Denim Du Jour Format 15 ml</meta_description>
<meta_keywords>vernis à ongles,morgantaylor,manucure,beauté des mains,nails,harmony</meta_keywords>
<meta_title></meta_title>
<link_rewrite>morgan-taylor-denim-du-jour</link_rewrite>
<name>Morgan Taylor Denim Du Jour</name>
<description><p>Vernis à Ongle Morgan Taylor Denim Du Jour 15 ml</p></description>
<description_short></description_short>
<available_now></available_now>
<available_later></available_later>
<id_lang>1</id_lang>
<id_shop>1</id_shop>
</object>
6.
1.
1.
1.
MTDENIMJR
0
0
0
0.05
0
815264500995
815264500995
0
0
0
9
1.
1.
0
0
1.
10.313
2.9
0
0
0
0
0
1.
404
0
1.
0000-00-00
新的
1.
1.
二者都
0
0
2013-02-27 08:03:35
2018-05-09 10:59:40
3.
0
36
VernisáOngle Morgan Taylor牛仔日装15毫升
vernisáongles、morgantaylor、manucure、beautédes Main、钉子、harmony
摩根·泰勒牛仔布
摩根·泰勒牛仔布
pVernisáOngle Morgan Taylor日牛仔15 ml/p
1.
1.
我从这个产品和所有其他产品中得到一个System.Xml.XmlException:“它们是多个根元素。第2行,位置2'
只有一个根元素:“object”,每个节点都是唯一的,我一直在尝试所有在线Xml检查器,我返回的Xml成功通过了每个测试,所以我有点疯狂
因此,如果有一些善良的灵魂会给我一个建议,或者一个开始的解释,或者只是指出我正在犯的一个巨大的错误,我会非常感激:)
万分感谢
Jeff所以错误来自linux服务器端,原因是**
@ini\u set('display\u errors','on')代码>**
一切又好起来了
感谢您的关心和支持
Jeff您能发布异常的完整堆栈跟踪吗?VS 2017 Enterprise提供了一个非常糟糕的信息:加载的“C:\WINDOWS\Microsoft.Net\assembly\GAC\U MSIL\System.Xml.resources\v4.0\U 4.0.0.0\U fr\U b77a5c561934e089\System.Xml.resources.dll”。模块是在没有符号的情况下构建的。引发异常:System.Xml.dll中的“System.Xml.XmlException”System.Xml.dllca中发生类型为“System.Xml.XmlException”的未经处理的异常。使用您提供的代码和第二个Xml,无法在我的计算机上复制此错误。您确定内容字符串与您在此处发布的XML完全相同吗?可能存在编码问题?可能是因为存在空节点:System.Xml.XmlTextReaderImpl.Throw(异常e)\r\náSystem.Xml.XmlTextReaderImpl.Throw(字符串res,字符串arg)\r\náSystem.Xml.XmlTextReaderImpl.ParseDocumentContent()\r\náSystem.Xml.XmlTextReaderImpl.Read()\r\náSystem.Xml.XmlLoader.LoadNode(布尔skipOverWhitespace)\r\náSystem.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)\r\náSystem.Xml.XmlLoader.Load(XmlDocument doc,XmlReader reader,布尔保留空白)\r\náSystem.Xml.XmlDocument.Load(XmlReader reader)\r\n