简体   繁体   中英

Need Help Deserializing a Xml document in c#

I need help deserializing a XML file that i got on my machine. i have tried somthing like this.

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];
}

but it says there is an error.

There is an error in XML-document (2, 2) System.InvalidOperationException: der is an error in XML-document(2,2): http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader'> was not expected.

I can post the XML document if it is needed but it is a long document. Hope some of you can help.

Here is a part of the XML document

<?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>

Try using xsd.exe for generating ac# class from this xml document.

There is an answer here: Generate C# class from XML

Then you can deserialize the xml document to the newly generated c# class

There are a few issues here that I can see. First, the XML you're trying to deserialize to a DataSet isn't a DataSet, so the deserialization would fail. Easiest way to do what you're trying to do is to make a set of POCOs that do represent your data, annotate them appropriately, and deserialize to those, and you can then use them as you would any other object. The exception you're getting, I believe is just that the XmlSerializer doesn't recognise your root element as being the one expected by the DataSet, or simply that it cannot correctly match the namespace. You can see more details about this here ( https://msdn.microsoft.com/en-us/library/aa302290.aspx#trblshtxsd_topic5 ). Details of controlling XML Serialization/Deserialization via annotated POCOs can be found here ( https://msdn.microsoft.com/en-us/library/2baksw0z(v=vs.110).aspx ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM