[英]Need Help Deserializing a Xml document in c#
我需要反序列化我在计算机上获得的XML文件的帮助。 我已经尝试过这样的事情。
private void button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataSet));
FileStream readStream = new FileStream("D:\\Europoultry\\Connection Hjælp\\CIN_Example_2.xml", FileMode.Open);
ds = (DataSet)xmlSerializer.Deserialize(readStream);
readStream.Close();
dataGridView1.DataSource = ds.Tables[0];
}
但它说有错误。
XML文档(2,2)System.InvalidOperationException中存在错误:der是XML文档(2,2)中的错误:http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader'>不是预期。
如果需要,我可以发布XML文档,但这是一个很长的文档。 希望你们中的一些人能提供帮助。
这是XML文档的一部分
<?xml version="1.0" encoding="utf-8"?>
<sh:StandardBusinessDocument xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" xmlns:eanucc="urn:ean.ucc:2" xmlns:gdsn="urn:ean.ucc:gdsn:2" xmlns:align="urn:ean.ucc:align:2" xmlns:chemical_ingredient="urn:ean.ucc:align:chemical_ingredient:2" xmlns:food_beverage_tobacco="urn:ean.ucc:align:food_beverage_tobacco:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader http://www.gdsregistry.org/2.8/schemas/sbdh/StandardBusinessDocumentHeader.xsd urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/CatalogueItemNotificationProxy.xsd urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/AttributeValuePairExtensionProxy.xsd urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/CaseLevelNonGTINLogisticsUnitExtensionProxy.xsd urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/TradeItemExtensionSpecificsProxy.xsd urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/ChemicalIngredientExtensionProxy.xsd urn:ean.ucc:2 http://www.gdsregistry.org/2.8/schemas/FoodAndBeverageTradeItemExtensionProxy.xsd">
<sh:StandardBusinessDocumentHeader>
<sh:HeaderVersion>1.0</sh:HeaderVersion>
<sh:Sender>
<sh:Identifier Authority="EAN.UCC">5790000011032</sh:Identifier>
</sh:Sender>
<sh:Receiver>
<sh:Identifier Authority="EAN.UCC">5790000500000</sh:Identifier>
</sh:Receiver>
<sh:DocumentIdentification>
<sh:Standard>EAN.UCC</sh:Standard>
<sh:TypeVersion>2.8</sh:TypeVersion>
<sh:InstanceIdentifier>DI-35346-34535-xt435345</sh:InstanceIdentifier>
<sh:Type>catalogueItemNotification</sh:Type>
<sh:CreationDateAndTime>2013-12-20T10:46:26+00:00</sh:CreationDateAndTime>
</sh:DocumentIdentification>
</sh:StandardBusinessDocumentHeader>
<eanucc:message>
<entityIdentification>
<uniqueCreatorIdentification>MSG-35346-34535-xt435345</uniqueCreatorIdentification>
<contentOwner>
<gln>5790000011032</gln>
</contentOwner>
</entityIdentification>
<eanucc:transaction>
<entityIdentification>
<uniqueCreatorIdentification>TRN-35346-34535-xt435345</uniqueCreatorIdentification>
<contentOwner>
<gln>5790000011032</gln>
</contentOwner>
</entityIdentification>
<command>
<eanucc:documentCommand>
<documentCommandHeader type="ADD">
<!--D8164-->
<entityIdentification>
<uniqueCreatorIdentification>CMD-35346-34535-xt435345</uniqueCreatorIdentification>
<contentOwner>
<gln>5790000011032</gln>
</contentOwner>
</entityIdentification>
</documentCommandHeader>
<documentCommandOperand>
<gdsn:catalogueItemNotification creationDateTime="2013-12-20T10:46:26+00:00" documentStatus="ORIGINAL" isReload="false">
<catalogueItem>
<catalogueItemState state="IN_PROGRESS"/>
<tradeItem>
<tradeItemUnitDescriptor>CASE</tradeItemUnitDescriptor>
<!--D8276-->
<tradeItemIdentification>
我可以看到一些问题。 首先,您要反序列化为DataSet的XML不是DataSet,因此反序列化将失败。 做您想做的事的最简单方法是制作一组POCO,它们可以代表您的数据,对其进行适当的注释,然后反序列化这些数据,然后您就可以像使用其他任何对象一样使用它们了。 我得到的例外是,我相信只是XmlSerializer不能将您的根元素识别为DataSet期望的元素,或者仅仅是它不能正确匹配名称空间。 您可以在此处( https://msdn.microsoft.com/zh-cn/library/aa302290.aspx#trblshtxsd_topic5 )中查看有关此内容的更多详细信息。 可以在此处找到通过带注释的POCO控制XML序列化/反序列化的详细信息( https://msdn.microsoft.com/zh-cn/library/2baksw0z(v=vs.110).aspx )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.