Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未将XML正确序列化到对象中_C#_Xml_Object_Serialization_Type Conversion - Fatal编程技术网

C# 未将XML正确序列化到对象中

C# 未将XML正确序列化到对象中,c#,xml,object,serialization,type-conversion,C#,Xml,Object,Serialization,Type Conversion,我试图序列化从第三方API返回的一段XML。然而,这样做时,我只是在序列化时检索对象的一部分。而且似乎只有一些价值观存在。我认为这可能是输入类型的问题,但是所有类型似乎都是正确的。我认为这可能与我的模型是如何构造的有关,在调试完代码后,我把它缩小为xml转换成对象的问题 C#代码: 正在返回的XML: <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> &l

我试图序列化从第三方API返回的一段XML。然而,这样做时,我只是在序列化时检索对象的一部分。而且似乎只有一些价值观存在。我认为这可能是输入类型的问题,但是所有类型似乎都是正确的。我认为这可能与我的模型是如何构造的有关,在调试完代码后,我把它缩小为xml转换成对象的问题

C#代码:

正在返回的XML:

 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <GetAvailabilityResponse xmlns="http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService">
         <GetAvailabilityResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <Status xmlns="http://schemas.datacontract.org/2004/07/InHouse.SharedLibraries.ServiceBase.BaseTypes">
               <Errors/>
               <HasErrors>false</HasErrors>
            </Status>
            <CSSDistrictCode>lv</CSSDistrictCode>
            <EADAvailability>
               <AvailabilityDetails i:type="EADAvailabilityDetails">
                  <Status xmlns="http://schemas.datacontract.org/2004/07/InHouse.SharedLibraries.ServiceBase.BaseTypes">
                     <Errors/>
                     <HasErrors>false</HasErrors>
                  </Status>
                  <EADAvailability>
                     <EADAvailabilityResult>
                        <CollectorNodeExchangeCode>NDMAI</CollectorNodeExchangeCode>
                        <CollectorNodeExchangeName>Maidstone</CollectorNodeExchangeName>
                        <Distance>0</Distance>
                        <EADBandwidth xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
                           <a:string>100M</a:string>
                        </EADBandwidth>
                        <EADSubType>EAD-LA</EADSubType>
                        <FibreExchangeCode>NDACO</FibreExchangeCode>
                        <FibreExchangename>Archers Court</FibreExchangename>
                        <IndicativeECC>Within tariff</IndicativeECC>
                        <IndicativeOrderCategory>Category 2.1</IndicativeOrderCategory>
                        <LocalExchangeCode>NDACO</LocalExchangeCode>
                        <LocalExchangeName>Archers Court</LocalExchangeName>
                        <ORLeadTime>40</ORLeadTime>
                        <OrderCategoryExplanation>There is a T node within 1km (or 650m in London) with spare capacity and ducting is in place, however some additional cabling is required between  the premises and the T node.</OrderCategoryExplanation>
                        <TTLeadTime>56</TTLeadTime>
                        <Zone>0</Zone>
                     </EADAvailabilityResult>
                     <EADAvailabilityResult>
                        <CollectorNodeExchangeCode>NDMAI</CollectorNodeExchangeCode>
                        <CollectorNodeExchangeName>Maidstone</CollectorNodeExchangeName>
                        <Distance>0</Distance>
                        <EADBandwidth xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
                           <a:string>LOW 1GB</a:string>
                        </EADBandwidth>
                        <EADSubType>EAD-LA</EADSubType>
                        <FibreExchangeCode>NDACO</FibreExchangeCode>
                        <FibreExchangename>Archers Court</FibreExchangename>
                        <IndicativeECC>Within tariff</IndicativeECC>
                        <IndicativeOrderCategory>Category 2.1</IndicativeOrderCategory>
                        <LocalExchangeCode>NDACO</LocalExchangeCode>
                        <LocalExchangeName>Archers Court</LocalExchangeName>
                        <ORLeadTime>40</ORLeadTime>
                        <OrderCategoryExplanation>There is a T node within 1km (or 650m in London) with spare capacity and ducting is in place, however some additional cabling is required between  the premises and the T node.</OrderCategoryExplanation>
                        <TTLeadTime>56</TTLeadTime>
                        <Zone>0</Zone>
                     </EADAvailabilityResult>
                     <EADAvailabilityResult>
                        <CollectorNodeExchangeCode>NDCAN</CollectorNodeExchangeCode>
                        <CollectorNodeExchangeName>Canterbury</CollectorNodeExchangeName>
                        <Distance>20656</Distance>
                        <EADBandwidth xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
                           <a:string>HIGH 1GB</a:string>
                        </EADBandwidth>
                        <EADSubType>EAD-NonLA</EADSubType>
                        <FibreExchangeCode>NDCAN</FibreExchangeCode>
                        <FibreExchangename>Canterbury</FibreExchangename>
                        <IndicativeECC>Within tariff</IndicativeECC>
                        <IndicativeOrderCategory>Category 2.1</IndicativeOrderCategory>
                        <LocalExchangeCode>NDACO</LocalExchangeCode>
                        <LocalExchangeName>Archers Court</LocalExchangeName>
                        <ORLeadTime>40</ORLeadTime>
                        <OrderCategoryExplanation>There is a T node within 1km (or 650m in London) with spare capacity and ducting is in place, however some additional cabling is required between  the premises and the T node.</OrderCategoryExplanation>
                        <TTLeadTime>56</TTLeadTime>
                        <Zone>B</Zone>
                     </EADAvailabilityResult>
                  </EADAvailability>
                  <LeadTime>10</LeadTime>
               </AvailabilityDetails>
            </EADAvailability>
            <GoldAddressKey>A00009292752</GoldAddressKey>
            <Postcode/>
         </GetAvailabilityResult>
      </GetAvailabilityResponse>
   </s:Body>
</s:Envelope>

错误的
吕
错误的
恩德迈
梅德斯通
0
100米
阿拉巴马州
恩达科
弓箭手球场
关税内
类别2.1
恩达科
弓箭手球场
40
1km(或伦敦650m)范围内有一个T节点,具有备用容量,且管道已安装到位,但在场地和T节点之间需要一些额外的电缆。
56
0
恩德迈
梅德斯通
0
低1GB
阿拉巴马州
恩达科
弓箭手球场
关税内
类别2.1
恩达科
弓箭手球场
40
1km(或伦敦650m)范围内有一个T节点,具有备用容量,且管道已安装到位,但在场地和T节点之间需要一些额外的电缆。
56
0
NDCAN
坎特伯雷
20656
高1GB
伊德诺拉酒店
恩德坎
坎特伯雷
关税内
类别2.1
恩达科
弓箭手球场
40
1km(或伦敦650m)范围内有一个T节点,具有备用容量,且管道已安装到位,但在场地和T节点之间需要一些额外的电缆。
56
B
10
A00009292752
型号:

 [Serializable, XmlRoot("AvailabilityDetails")]
public class EADAvailabilityDetails
{
    [XmlElement("EADAvailability")]
    public EADAvailability EADAvailability { get; set; }
}

public class EADAvailability
{
    [XmlElement("EADAvailabilityResult")]
    public List<EADAvailabilityResult> EADAvailabilityResult { get; set; }
}

public class EADAvailabilityResult
{
    [XmlElement("CollectorNodeExchangeCode")]
    public string CollectorNodeExchangeCode { get; set; }

    [XmlElement("CollectorNodeExchangeName")]
    public string CollectorNodeExchangeName { get; set; }

    [XmlElement("Distance")]
    public int Distance { get; set; }

    [XmlElement("EADBandwidth")]
    public string EADBandwidth { get; set; }

    [XmlElement("EADSubType")]
    public string EADSubType { get; set; }

    [XmlElement("FibreExchangeCode")]
    public string FibreExchangeCode { get; set; }

    [XmlElement("FibreExchangename")]
    public string FibreExchangename { get; set; }

    [XmlElement("IndicativeECC")]
    public string IndicativeECC { get; set; }

    [XmlElement("IndicativeOrderCategory")]
    public string IndicativeOrderCategory { get; set; }

    [XmlElement("LocalExchangeCode")]
    public string LocalExchangeCode { get; set; }

    [XmlElement("LocalExchangeName")]
    public string LocalExchangeName { get; set; }

    [XmlElement("ORLeadTime")]
    public int ORLeadTime { get; set; }

    [XmlElement("OrderCategoryExplanation")]
    public string OrderCategoryExplanation { get; set; }

    [XmlElement("TTLeadTime")]
    public int TTLeadTime { get; set; }

    [XmlElement("Zone")]
    public int Zone { get; set; }
}
[可序列化,XmlRoot(“可用性详细信息”)]
公共类可用性详细信息
{
[XmlElement(“可用性”)]
公共可用性可用性可用性{get;set;}
}
公共类可用性
{
[XmlElement(“EADAvaailabilityresult”)]
公共列表eadavaailabilityresult{get;set;}
}
公共类可用性结果
{
[XmlElement(“CollectorNodeExchangeCode”)]
公共字符串CollectorNodeExchangeCode{get;set;}
[XmlElement(“CollectorNodeExchangeName”)]
公共字符串CollectorNodeExchangeName{get;set;}
[XmlElement(“距离”)]
公共整数距离{get;set;}
[XmlElement(“带宽”)]
公共字符串{get;set;}
[XmlElement(“EADSubType”)]
公共字符串子类型{get;set;}
[XmlElement(“光纤交换代码”)]
公共字符串FibreExchange代码{get;set;}
[XmlElement(“光纤交换名称”)]
公共字符串FibreExchangename{get;set;}
[XmlElement(“指示性EECC”)]
公共字符串指示性eecc{get;set;}
[XmlElement(“指示性顺序分类”)]
公共字符串指示性OrderCategory{get;set;}
[XmlElement(“LocalExchangeCode”)]
公共字符串LocalExchangeCode{get;set;}
[XmlElement(“LocalExchangeName”)]
公共字符串LocalExchangeName{get;set;}
[XmlElement(“ORLeadTime”)]
public int或readtime{get;set;}
[XmlElement(“OrderCategoryExplanation”)]
公共字符串OrderCategoryExplanation{get;set;}
[XmlElement(“TTLeadTime”)]
公共时间{get;set;}
[XmlElement(“区域”)]
公共int区域{get;set;}
}
序列化后的XML:

{
"<EADAvailability>k__BackingField": {
    "EADAvailabilityResult": [
        {
            "CollectorNodeExchangeCode": "NDMAI",
            "CollectorNodeExchangeName": "Maidstone",
            "Distance": 0,
            "EADBandwidth": "100M",
            "EADSubType": null,
            "FibreExchangeCode": null,
            "FibreExchangename": null,
            "IndicativeECC": null,
            "IndicativeOrderCategory": null,
            "LocalExchangeCode": null,
            "LocalExchangeName": null,
            "ORLeadTime": 0,
            "OrderCategoryExplanation": null,
            "TTLeadTime": 0,
            "Zone": 0
        }
    ]
}
{
“k__BackingField”:{
“EADAvaailabilityresult”:[
{
“CollectorNodeExchangeCode”:“NDMAI”,
“CollectorNodeExchangeName”:“Maidstone”,
“距离”:0,
“带宽”:“100M”,
“EADSubType”:空,
“光纤交换代码”:空,
“光纤交换名称”:空,
“指示性EECC”:空,
“指示性订单类别”:空,
“LocalExchangeCode”:空,
“LocalExchangeName”:null,
“ORLeadTime”:0,
“OrderCategoryExplanation”:空,
“时间”:0,
“区域”:0
}
]
}
}以下代码有效:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication139
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));

            Envelope envelope = (Envelope)serializer.Deserialize(reader);

        }
    }
    [XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement (Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
    }
    public class Body
    {
        [XmlElement(Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        public GetAvailabilityResponse GetAvailabilityResponse { get; set; }
    }
    public class GetAvailabilityResponse
    {
        [XmlElement(Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        public GetAvailabilityResult GetAvailabilityResult { get; set; }
    }
    public class GetAvailabilityResult
    {
        [XmlArray("EADAvailability", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        [XmlArrayItem("AvailabilityDetails", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        public AvailabilityDetails[] AvailabilityDetails { get; set; }
    }
    [XmlInclude(typeof(EADAvailabilityDetails))]
    [Serializable, XmlRoot("AvailabilityDetails", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
    public class AvailabilityDetails
    {
    }
    [Serializable, XmlRoot("EADAvailabilityDetails", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
    public class EADAvailabilityDetails : AvailabilityDetails
    {
        [XmlArray("EADAvailability", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        [XmlArrayItem("EADAvailabilityResult", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        public EADAvailabilityResult[] EADAvailabilityResult { get; set; }
    }
    public class EADAvailabilityResult
    {
        public string CollectorNodeExchangeCode { get; set; }
        public string CollectorNodeExchangeName { get; set; }
        public int Distance { get; set; }
        public EADBandwidth EADBandwidth { get; set; }
        public string EADSubType { get; set; }
        public string FibreExchangeCode { get; set; }
        public string FibreExchangename { get; set; }
        public string IndicativeECC { get; set; }
        public string IndicativeOrderCategory { get; set; }
        public string LocalExchangeCode { get; set; }
        public string LocalExchangeName { get; set; }
        public int ORLeadTime { get; set; }
        public string OrderCategoryExplanation { get; set; }
        public int TTLeadTime { get; set; }
        public string Zone { get; set; }
    }
    public class EADBandwidth
    {
        [XmlElement(ElementName = "string", Type = typeof(string), Namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays")]
        public string String { get; set; }
    }







}

“序列化后的XML”实际上是JSON。JSON是否表示您已设法从xml反序列化的内容?能否显示代码中生成“序列化后的xml”的部分?我只关注“EADAvailability”节点中的属性。我已经在我的模型中列出了。问题是,只有一些属性返回时包含值,而有些属性返回为null,而它们本不应该返回。这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication139
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));

            Envelope envelope = (Envelope)serializer.Deserialize(reader);

        }
    }
    [XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement (Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
    }
    public class Body
    {
        [XmlElement(Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        public GetAvailabilityResponse GetAvailabilityResponse { get; set; }
    }
    public class GetAvailabilityResponse
    {
        [XmlElement(Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        public GetAvailabilityResult GetAvailabilityResult { get; set; }
    }
    public class GetAvailabilityResult
    {
        [XmlArray("EADAvailability", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        [XmlArrayItem("AvailabilityDetails", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        public AvailabilityDetails[] AvailabilityDetails { get; set; }
    }
    [XmlInclude(typeof(EADAvailabilityDetails))]
    [Serializable, XmlRoot("AvailabilityDetails", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
    public class AvailabilityDetails
    {
    }
    [Serializable, XmlRoot("EADAvailabilityDetails", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
    public class EADAvailabilityDetails : AvailabilityDetails
    {
        [XmlArray("EADAvailability", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        [XmlArrayItem("EADAvailabilityResult", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        public EADAvailabilityResult[] EADAvailabilityResult { get; set; }
    }
    public class EADAvailabilityResult
    {
        public string CollectorNodeExchangeCode { get; set; }
        public string CollectorNodeExchangeName { get; set; }
        public int Distance { get; set; }
        public EADBandwidth EADBandwidth { get; set; }
        public string EADSubType { get; set; }
        public string FibreExchangeCode { get; set; }
        public string FibreExchangename { get; set; }
        public string IndicativeECC { get; set; }
        public string IndicativeOrderCategory { get; set; }
        public string LocalExchangeCode { get; set; }
        public string LocalExchangeName { get; set; }
        public int ORLeadTime { get; set; }
        public string OrderCategoryExplanation { get; set; }
        public int TTLeadTime { get; set; }
        public string Zone { get; set; }
    }
    public class EADBandwidth
    {
        [XmlElement(ElementName = "string", Type = typeof(string), Namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays")]
        public string String { get; set; }
    }







}