如何反序列化xml

Posted

技术标签:

【中文标题】如何反序列化xml【英文标题】:How to deserialize xml 【发布时间】:2019-04-24 05:42:50 【问题描述】:

我正在加载 xml 文件,例如

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

做2个班

 [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;
    

我正在加载 xml 文件 (temp.xml),然后在类 2 Position 和 Head 上进行序列化程序 然后是 StringReader,它从文件中读取字符串 然后var position = (Positions) serializer.Deserialize (reader); 类序列化变为反序列化

    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);

但我看到了错误 错误消息 XML 文档 (1, 1) 中存在错误。 在哪里?:在 System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,字符串 encodingStyle,XmlDeserializationEvents 事件) 在 System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader) 在 .cs:line 64 (var position = (Positions)serializer.Deserialize(reader);)

如何正确反序列化这个xml?

【问题讨论】:

您的代码 sn-ps 不加起来。您发布的内容无法编译。请发帖minimal reproducible example。 【参考方案1】:

试试:

[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;

用途:

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

您只需使用OrderHeadUpload 反序列化一次。如果您有多个职位,您可以声明为List&lt;T&gt;

【讨论】:

以上是关于如何反序列化xml的主要内容,如果未能解决你的问题,请参考以下文章

如何反序列化xml

如何反序列化 C# 中只有属性的 xml 元素?

如何从 nusoap 服务返回的 XML 反序列化对象?

如何反序列化 System.Xml.Linq.XElement?

如何使用 FasterXML 反序列化带有注释的 XML

如何反序列化java Long哪个xml标签有xsi:nil?