C# 将XML反序列化为列表<&燃气轮机;

C# 将XML反序列化为列表<&燃气轮机;,c#,.net,C#,.net,我在将一些XML反序列化为强类型列表时遇到了一个问题。问题是,即使在XML中有列表,但列表中没有任何内容 我已经把代码贴在下面了 我的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/">

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

我已经把代码贴在下面了

我的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>
PushDataArray为空,但不是null。为什么

多谢各位


(Stackoverflow应该根据post消息的详细信息检查他们的算法)

据我所知,不需要为
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; }
}
[XmlRoot(ElementName=“pushDataArray”)]
公共类pushDataArray
{
[xmlement(ElementName=“assetId”)]
公共字符串AssetId{get;set;}
[xmlement(ElementName=“assetName”)]
公共字符串AssetName{get;set;}
[XmlElement(ElementName=“boxData”)]
public BoxData BoxData{get;set;}
[XmlElement(ElementName=“externalCustomerIdentification”)]
公共字符串ExternalCustomerIdentification{get;set;}
}
[XmlRoot(ElementName=“Body”,
名称空间=”http://schemas.xmlsoap.org/soap/envelope/")]
公共阶级团体
{
[XmlArray(ElementName=“PushDataArray”,
名称空间=”http://sender.push.ws.nicbase.com/")]
[XmlArrayItem(“pushDataArray”)]
公共列表PushDataArray{get;set;}
}

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

<boxdata></boxdata>
<boxdata/>

我用上面的xml数据和类尝试了您的代码,它很有效。试试看。

是的,我现在明白了。但我的实际XML是正确的。这里发生了一个粘贴错误。非常抱歉。Will fix It正是那些名称空间属性起了作用!这个XML文件和类来自哪里?如果这是SOAP消息,则可以使用svcutil.exe或wsdl.exe直接从wsdl文件创建代理类和DTO。这种方式不会出现语法错误一般来说,只需在XML文件上打开一个FileStream,并将其与
反序列化一起使用即可。没有理由这样使用XmlDocument和MemoryStream。最有可能的是,双重转换消除了任何有问题的XML元素
[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; }
}
<boxdata></boxdata>
<boxdata/>
</boxdata>
<?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; }
}