将XML反序列化为List <>

问题描述 投票:1回答:2

我有一个问题,将一些XML反序列化为强类型列表。问题是我的列表中没有任何内容,即使我在XML中也有。

我已经发布了以下代码。

我们是CML:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header />
   <S:Body>
      <ns2:PushDataArray xmlns:ns2="http://sender.push.ws.nicbase.com/">
         <pushDataArray>
            <assetId>00000993</assetId>
            <assetName>Some name</assetName>
            </boxData>
            <externalCustomerIdentification>DFDS</externalCustomerIdentification>
         </pushDataArray>
        <pushDataArray>
            <assetId>00000993</assetId>
            <assetName>Some name</assetName>
            </boxData>
            <externalCustomerIdentification>DFDS</externalCustomerIdentification>
         </pushDataArray>
         <pushDataArray>
            <assetId>00000993</assetId>
            <assetName>Some name</assetName>
            </boxData>
            <externalCustomerIdentification>DFDS</externalCustomerIdentification>
         </pushDataArray>
      </ns2:PushDataArray>
   </S:Body>
</S:Envelope>

我的类型:

    [XmlRoot(ElementName = "pushDataArray")]
    public class pushDataArray
    {
        [XmlElement(ElementName = "assetId")]
        public string AssetId { get; set; }
        [XmlElement(ElementName = "assetName")]
        public string AssetName { get; set; }
        [XmlElement(ElementName = "boxData")]
        public BoxData BoxData { get; set; }
        [XmlElement(ElementName = "externalCustomerIdentification")]
        public string ExternalCustomerIdentification { get; set; }
    }

    [XmlRoot(ElementName = "PushDataArray", Namespace = "http://sender.push.ws.nicbase.com/")]
    public class PushDataArray
    {
        [XmlElement(ElementName = "pushDataArray")]
        public List<pushDataArray> pushDataArray { get; set; }
        [XmlAttribute(AttributeName = "ns2", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Ns2 { get; set; }
    }

    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    {
        [XmlElement(ElementName = "PushDataArray", Namespace = "http://sender.push.ws.nicbase.com/")]
        public PushDataArray PushDataArray { get; set; }
    }

    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public string Header { get; set; }
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName = "S", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string S { get; set; }
        [XmlAttribute(AttributeName = "SOAP-ENV", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string SOAPENV { get; set; }
    }

我在C#中的序列化代码

        var xmlFile = Environment.CurrentDirectory + "\\xmlfile1.xml";

        XmlDocument doc = new XmlDocument();
        doc.Load(xmlFile);

        MemoryStream xmlStream = new MemoryStream();
        doc.Save(xmlStream);

        xmlStream.Flush();//Adjust this if you want read your data 
        xmlStream.Position = 0;

        var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Envelope));

        var outboundMessage = serializer.Deserialize(xmlStream);

PushDataArray是空的,但不是null。怎么会 ?

谢谢。

(Stackoverflow应根据发布消息的详细信息检查其算法)

c# .net
2个回答
1
投票

你的XML数据是错误的。反序列化时没有异常? Boxdata元素在开始之前关闭。正确的例子:

<boxdata></boxdata>
<boxdata/>

您的案例如下:

</boxdata>

所以你应该尝试序列化的正确xml应该是:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header />
   <S:Body>
      <ns2:PushDataArray xmlns:ns2="http://sender.push.ws.nicbase.com/">
         <pushDataArray>
            <assetId>00000993</assetId>
            <assetName>Some name</assetName>
            <boxData/>
            <externalCustomerIdentification>DFDS</externalCustomerIdentification>
         </pushDataArray>
        <pushDataArray>
            <assetId>00000993</assetId>
            <assetName>Some name</assetName>
            <boxData/>
            <externalCustomerIdentification>DFDS</externalCustomerIdentification>
         </pushDataArray>
         <pushDataArray>
            <assetId>00000993</assetId>
            <assetName>Some name</assetName>
            <boxData/>
            <externalCustomerIdentification>DFDS</externalCustomerIdentification>
         </pushDataArray>
      </ns2:PushDataArray>
   </S:Body>
</S:Envelope>

同时将您的类修改为以下内容:

[SerializableAttribute()]
[XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{
    public object Header { get; set; }
    public EnvelopeBody Body { get; set; }
}

[SerializableAttribute()]
[XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{
    [XmlArrayAttribute(Namespace = "http://sender.push.ws.nicbase.com/")]
    [XmlArrayItemAttribute("pushDataArray", Namespace = "", IsNullable = false)]
    public pushDataArray[] PushDataArray { get; set; }
}

[SerializableAttribute()]
[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class pushDataArray
{
    public ushort assetId { get; set; }

    public string assetName { get; set; }

    public object boxData { get; set; }

    public string externalCustomerIdentification { get; set; }
}

我尝试了上面的xml数据和类的代码,它的工作原理。试试看。


0
投票

根据我的理解,不需要为PushDataArray定义单独的类型,而只需使用如下列表。

[XmlRoot(ElementName = "pushDataArray")]
public class pushDataArray
{
    [XmlElement(ElementName = "assetId")]
    public string AssetId { get; set; }
    [XmlElement(ElementName = "assetName")]
    public string AssetName { get; set; }
    [XmlElement(ElementName = "boxData")]
    public BoxData BoxData { get; set; }
    [XmlElement(ElementName = "externalCustomerIdentification")]
    public string ExternalCustomerIdentification { get; set; }
}

[XmlRoot(ElementName = "Body",
         Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
    [XmlArray(ElementName = "PushDataArray",
              Namespace = "http://sender.push.ws.nicbase.com/")]
    [XmlArrayItem("pushDataArray")]
    public List<pushDataArray> PushDataArray { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.