0
votes

I am load xml file like

<?xml version="1.0" encoding="UTF-8"?>
<OrderHeadUpload xmlns="http://www.sample-package.org" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Number>Z-00006453580748</Number>
    <Number_confirm>000033477</Number_confirm>
    <Positions>
        <id>168816</Product_id>
        <Tare_id>2442</Tare_id>
    </Positions>
</OrderHeadUpload>

Make 2 class

 [XmlRoot(ElementName = "Head", Namespace = "http://www.sample-package.org")]
    public class OrderHeadUpload
    {
        [XmlElement(ElementName = "Number_confirm", Namespace = "http://www.sample-package.org")]
        public string Client_id { get; set; }
        [XmlElement(ElementName = "Number", Namespace = "http://www.sample-package.org")]
        public string Barcode_id { get; set; }
    }

[XmlRoot(ElementName = "Positions", Namespace = "http://www.sample-package.org")]
    public class Positions
    {
        [XmlElement(ElementName = "id", Namespace = "http://www.sample-package.org")]
        public string id { get; set; }
        [XmlElement(ElementName = "Tare_id", Namespace = "http://www.sample-package.org")]
        public string  Tare_id {get; set;}
    }

i am load xml file (temp.xml) then serializer on classes 2 Position and Head then StringReader, which reads strings from a file and then var position = (Positions) serializer.Deserialize (reader); class serialized become deserialized

    doc.Load(temp);

    XmlSerializer serializer = new XmlSerializer(typeof(Positions));
    XmlSerializer serializer2 = new XmlSerializer(typeof(Head));

    StringReader reader = new StringReader(temp);

    var position = (Positions)serializer.Deserialize(reader);
    var Head = (OrderHeadUpload)serializer2.Deserialize(reader);

but i see error Error msg There is an error in XML document (1, 1). Where?: at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader) at .cs:line 64 (var position = (Positions)serializer.Deserialize(reader);)

How to correctly deserialize this xml?

1
Your code snippets don't add up. What you posted would not compile. Please post a minimal reproducible example.nvoigt

1 Answers

0
votes

Try:

[XmlRoot(ElementName = "Positions", Namespace = "http://www.sample-package.org")]
public class Positions
{
    [XmlElement(ElementName = "id", Namespace = "http://www.sample-package.org")]
    public string id { get; set; }
    [XmlElement(ElementName = "Tare_id", Namespace = "http://www.sample-package.org")]
    public string Tare_id { get; set; }
}

[XmlRoot(ElementName = "Head", Namespace = "http://www.sample-package.org")]
public class OrderHeadUpload
{
    [XmlElement(ElementName = "Number_confirm", Namespace = "http://www.sample-package.org")]
    public string Client_id { get; set; }
    [XmlElement(ElementName = "Number", Namespace = "http://www.sample-package.org")]
    public string Barcode_id { get; set; }

    [XmlElement(ElementName = "Positions")]
    public Positions positions;
}

Use:

var order = (OrderHeadUpload)serializer.Deserialize(reader);

And you need to deserialize just onetime with OrderHeadUpload. If you have more than one Positions, you can declare like a List<T>