我需要帮助反序列化我在我的机器上获得的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 39;>没想到。
如果需要,我可以发布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>
答案 0 :(得分:1)
答案 1 :(得分:0)
我可以看到这里有一些问题。首先,您尝试反序列化为DataSet的XML不是DataSet,因此反序列化将失败。做你想要做的最简单的方法是制作一组代表你的数据的POCO,对它们进行适当的注释,然后反序列化,然后你可以像使用任何其他对象一样使用它们。你得到的例外情况,我相信只是XmlSerializer不能将你的根元素识别为DataSet所期望的根元素,或者只是它无法正确匹配命名空间。您可以在此处查看有关此内容的更多详细信息(https://msdn.microsoft.com/en-us/library/aa302290.aspx#trblshtxsd_topic5)。可以在此处找到通过带注释的POCO控制XML序列化/反序列化的详细信息(https://msdn.microsoft.com/en-us/library/2baksw0z(v=vs.110).aspx)。