繁体   English   中英

将 xml 序列化为 c# object

[英]Serialize xml to c# object

我在尝试将 XML 序列化为 C# object 时遇到了一些问题。 我收到多个“System.InvalidOperationException”。

错误:

  • InvalidOperationException:反映属性“MPFAvailability”时出现错误。

  • InvalidOperationException:反映类型“TalkTalkAPI.Models.AvailabilityDetails”的错误。

  • InvalidOperationException:反映属性“AvailabilityContent”时出现错误。

  • InvalidOperationException:反映类型“TalkTalkAPI.Models.AvailabilityContent”的错误。

我已经查看了内部异常,但不存在进一步的细节。 我确信这与我的 object 的设置方式有关。

XML:

<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>
        <GoldAddressKey>A00009292705</GoldAddressKey>
        <MPFAvailability>
           <AvailabilityDetails i:type="MPFAvailabilityDetails">
              <Status xmlns="http://schemas.datacontract.org/2004/07/InHouse.SharedLibraries.ServiceBase.BaseTypes">
                 <Errors/>
                 <HasErrors>false</HasErrors>
              </Status>
              <AccessLineID>PRH4144561P4</AccessLineID>
              <AccessLineStatus>BTLive</AccessLineStatus>
              <AccessLineStatusVDSL>Working</AccessLineStatusVDSL>
              <DPType>External</DPType>
              <EstimatedSyncSpeed>
                 <AnnexADownstream>8818</AnnexADownstream>
                 <AnnexAMaxRangeDownstream>11055</AnnexAMaxRangeDownstream>
                 <AnnexAMinRangeDownstream>6464</AnnexAMinRangeDownstream>
                 <AnnexMDownstream>7318</AnnexMDownstream>
                 <AnnexMMaxRangeDownstream>14655</AnnexMMaxRangeDownstream>
                 <AnnexMMaxRangeUpstream>1015</AnnexMMaxRangeUpstream>
                 <AnnexMMinRangeDownstream>7945</AnnexMMinRangeDownstream>
                 <AnnexMMinRangeUpstream>876</AnnexMMinRangeUpstream>
                 <AnnexMUpstream>1500</AnnexMUpstream>
                 <MinThresholdAnnexADownstream>5129</MinThresholdAnnexADownstream>
                 <MinThresholdAnnexMDownStream>6423</MinThresholdAnnexMDownStream>
              </EstimatedSyncSpeed>
              <EstimatedThroughputSpeedAnnexA>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>5.43</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>4.31</DownstreamThroughputMin>
                    <DownstreamThroughputTop>9.29</DownstreamThroughputTop>
                    <SpeedCategory>Consumer</SpeedCategory>
                    <TimeOfDay>Peak</TimeOfDay>
                    <UpstreamThroughputBottom>0.74</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.5</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.85</UpstreamThroughputTop>
                 </ThroughputSpeedType>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>5.53</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>4.39</DownstreamThroughputMin>
                    <DownstreamThroughputTop>9.46</DownstreamThroughputTop>
                    <SpeedCategory>Consumer</SpeedCategory>
                    <TimeOfDay>OffPeak</TimeOfDay>
                    <UpstreamThroughputBottom>0.75</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.51</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.87</UpstreamThroughputTop>
                 </ThroughputSpeedType>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>5.53</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>4.39</DownstreamThroughputMin>
                    <DownstreamThroughputTop>9.46</DownstreamThroughputTop>
                    <SpeedCategory>Business</SpeedCategory>
                    <TimeOfDay>Peak</TimeOfDay>
                    <UpstreamThroughputBottom>0.75</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.51</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.87</UpstreamThroughputTop>
                 </ThroughputSpeedType>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>5.57</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>4.42</DownstreamThroughputMin>
                    <DownstreamThroughputTop>9.53</DownstreamThroughputTop>
                    <SpeedCategory>Business</SpeedCategory>
                    <TimeOfDay>OffPeak</TimeOfDay>
                    <UpstreamThroughputBottom>0.76</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.51</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.88</UpstreamThroughputTop>
                 </ThroughputSpeedType>
              </EstimatedThroughputSpeedAnnexA>
              <EstimatedThroughputSpeedAnnexM>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>6.67</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>5.4</DownstreamThroughputMin>
                    <DownstreamThroughputTop>12.31</DownstreamThroughputTop>
                    <SpeedCategory>Consumer</SpeedCategory>
                    <TimeOfDay>Peak</TimeOfDay>
                    <UpstreamThroughputBottom>0.74</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.5</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.85</UpstreamThroughputTop>
                 </ThroughputSpeedType>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>6.8</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>5.5</DownstreamThroughputMin>
                    <DownstreamThroughputTop>12.55</DownstreamThroughputTop>
                    <SpeedCategory>Consumer</SpeedCategory>
                    <TimeOfDay>OffPeak</TimeOfDay>
                    <UpstreamThroughputBottom>0.75</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.51</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.87</UpstreamThroughputTop>
                 </ThroughputSpeedType>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>6.8</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>5.5</DownstreamThroughputMin>
                    <DownstreamThroughputTop>12.55</DownstreamThroughputTop>
                    <SpeedCategory>Business</SpeedCategory>
                    <TimeOfDay>Peak</TimeOfDay>
                    <UpstreamThroughputBottom>0.75</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.51</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.87</UpstreamThroughputTop>
                 </ThroughputSpeedType>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>6.85</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>5.54</DownstreamThroughputMin>
                    <DownstreamThroughputTop>12.63</DownstreamThroughputTop>
                    <SpeedCategory>Business</SpeedCategory>
                    <TimeOfDay>OffPeak</TimeOfDay>
                    <UpstreamThroughputBottom>0.76</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.51</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.88</UpstreamThroughputTop>
                 </ThroughputSpeedType>
              </EstimatedThroughputSpeedAnnexM>
              <ExchangeDetails>
                 <CSSExchangeDistrictCode>ND</CSSExchangeDistrictCode>
                 <CircuitLineLength>
                    <Length>1903</Length>
                    <MeasureType>CircuitEstimated</MeasureType>
                 </CircuitLineLength>
                 <ExchangeCapacity>G</ExchangeCapacity>
                 <ExchangeCode>NDACO</ExchangeCode>
                 <ExchangeName>Archers Court</ExchangeName>
                 <ExchangeState>Live</ExchangeState>
                 <ForecastDate>2010-05-12T00:00:00</ForecastDate>
                 <ProductMessages i:nil="true"/>
              </ExchangeDetails>
              <GoldAddressKey>A00009292705</GoldAddressKey>
              <LineTypeDetails>
                 <WorkingLineDetails>
                    <InstallationType>Standard</InstallationType>
                    <ProductType>MPF</ProductType>
                 </WorkingLineDetails>
              </LineTypeDetails>
              <TechnologyMessages>
                 <TechnologyMessage>
                    <Code>LLUMPF</Code>
                    <Message>MPF LLU Installation</Message>
                 </TechnologyMessage>
                 <TechnologyMessage>
                    <Code>GEA</Code>
                    <Message>Generic Ethernet Access</Message>
                 </TechnologyMessage>
              </TechnologyMessages>
           </AvailabilityDetails>
        </MPFAvailability>
        <Postcode/>
        <SMPFAvailability>
           <AvailabilityDetails i:type="SMPFAvailabilityDetails">
              <Status xmlns="http://schemas.datacontract.org/2004/07/InHouse.SharedLibraries.ServiceBase.BaseTypes">
                 <Errors/>
                 <HasErrors>false</HasErrors>
              </Status>
              <AccessLineID>PRH4144561P4</AccessLineID>
              <AccessLineStatus>BTLive</AccessLineStatus>
              <AccessLineStatusVDSL>Working</AccessLineStatusVDSL>
              <EstimatedSyncSpeed>
                 <AnnexADownstream>8818</AnnexADownstream>
                 <AnnexAMaxRangeDownstream>11055</AnnexAMaxRangeDownstream>
                 <AnnexAMinRangeDownstream>6464</AnnexAMinRangeDownstream>
                 <AnnexMDownstream>7318</AnnexMDownstream>
                 <AnnexMMaxRangeDownstream>14655</AnnexMMaxRangeDownstream>
                 <AnnexMMaxRangeUpstream>1015</AnnexMMaxRangeUpstream>
                 <AnnexMMinRangeDownstream>7945</AnnexMMinRangeDownstream>
                 <AnnexMMinRangeUpstream>876</AnnexMMinRangeUpstream>
                 <AnnexMUpstream>1500</AnnexMUpstream>
                 <MinThresholdAnnexADownstream>5129</MinThresholdAnnexADownstream>
                 <MinThresholdAnnexMDownStream>6423</MinThresholdAnnexMDownStream>
              </EstimatedSyncSpeed>
              <EstimatedThroughputSpeedAnnexA>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>5.43</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>4.31</DownstreamThroughputMin>
                    <DownstreamThroughputTop>9.29</DownstreamThroughputTop>
                    <SpeedCategory>Consumer</SpeedCategory>
                    <TimeOfDay>Peak</TimeOfDay>
                    <UpstreamThroughputBottom>0.74</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.5</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.85</UpstreamThroughputTop>
                 </ThroughputSpeedType>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>5.53</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>4.39</DownstreamThroughputMin>
                    <DownstreamThroughputTop>9.46</DownstreamThroughputTop>
                    <SpeedCategory>Consumer</SpeedCategory>
                    <TimeOfDay>OffPeak</TimeOfDay>
                    <UpstreamThroughputBottom>0.75</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.51</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.87</UpstreamThroughputTop>
                 </ThroughputSpeedType>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>5.53</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>4.39</DownstreamThroughputMin>
                    <DownstreamThroughputTop>9.46</DownstreamThroughputTop>
                    <SpeedCategory>Business</SpeedCategory>
                    <TimeOfDay>Peak</TimeOfDay>
                    <UpstreamThroughputBottom>0.75</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.51</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.87</UpstreamThroughputTop>
                 </ThroughputSpeedType>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>5.57</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>4.42</DownstreamThroughputMin>
                    <DownstreamThroughputTop>9.53</DownstreamThroughputTop>
                    <SpeedCategory>Business</SpeedCategory>
                    <TimeOfDay>OffPeak</TimeOfDay>
                    <UpstreamThroughputBottom>0.76</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.51</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.88</UpstreamThroughputTop>
                 </ThroughputSpeedType>
              </EstimatedThroughputSpeedAnnexA>
              <EstimatedThroughputSpeedAnnexM>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>6.67</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>5.4</DownstreamThroughputMin>
                    <DownstreamThroughputTop>12.31</DownstreamThroughputTop>
                    <SpeedCategory>Consumer</SpeedCategory>
                    <TimeOfDay>Peak</TimeOfDay>
                    <UpstreamThroughputBottom>0.74</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.5</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.85</UpstreamThroughputTop>
                 </ThroughputSpeedType>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>6.8</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>5.5</DownstreamThroughputMin>
                    <DownstreamThroughputTop>12.55</DownstreamThroughputTop>
                    <SpeedCategory>Consumer</SpeedCategory>
                    <TimeOfDay>OffPeak</TimeOfDay>
                    <UpstreamThroughputBottom>0.75</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.51</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.87</UpstreamThroughputTop>
                 </ThroughputSpeedType>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>6.8</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>5.5</DownstreamThroughputMin>
                    <DownstreamThroughputTop>12.55</DownstreamThroughputTop>
                    <SpeedCategory>Business</SpeedCategory>
                    <TimeOfDay>Peak</TimeOfDay>
                    <UpstreamThroughputBottom>0.75</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.51</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.87</UpstreamThroughputTop>
                 </ThroughputSpeedType>
                 <ThroughputSpeedType>
                    <DownstreamThroughputBottom>6.85</DownstreamThroughputBottom>
                    <DownstreamThroughputMin>5.54</DownstreamThroughputMin>
                    <DownstreamThroughputTop>12.63</DownstreamThroughputTop>
                    <SpeedCategory>Business</SpeedCategory>
                    <TimeOfDay>OffPeak</TimeOfDay>
                    <UpstreamThroughputBottom>0.76</UpstreamThroughputBottom>
                    <UpstreamThroughputMin>0.51</UpstreamThroughputMin>
                    <UpstreamThroughputTop>0.88</UpstreamThroughputTop>
                 </ThroughputSpeedType>
              </EstimatedThroughputSpeedAnnexM>
              <ExchangeDetails>
                 <CSSExchangeDistrictCode>ND</CSSExchangeDistrictCode>
                 <CircuitLineLength>
                    <Length>1903</Length>
                    <MeasureType>CircuitEstimated</MeasureType>
                 </CircuitLineLength>
                 <ExchangeCapacity>G</ExchangeCapacity>
                 <ExchangeCode>NDACO</ExchangeCode>
                 <ExchangeName>Archers Court</ExchangeName>
                 <ExchangeState>Live</ExchangeState>
                 <ForecastDate>0001-01-01T00:00:00</ForecastDate>
                 <ProductMessages i:nil="true"/>
              </ExchangeDetails>
              <GoldAddressKey>A00009292705</GoldAddressKey>
              <LineTypeDetails>
                 <WorkingLineDetails>
                    <InstallationType>Standard</InstallationType>
                    <ProductType>MPF</ProductType>
                 </WorkingLineDetails>
              </LineTypeDetails>
              <TechnologyMessages>
                 <TechnologyMessage>
                    <Code>LLUMPF</Code>
                    <Message>MPF LLU Installation</Message>
                 </TechnologyMessage>
                 <TechnologyMessage>
                    <Code>GEA</Code>
                    <Message>Generic Ethernet Access</Message>
                 </TechnologyMessage>
              </TechnologyMessages>
           </AvailabilityDetails>
        </SMPFAvailability>
     </GetAvailabilityResult>

Model:

[Serializable, XmlRoot("GetAvailabilityResult")]
public class Availability
{
    [XmlElement("MPFAvailability")]
    public AvailabilityDetails MPFAvailability { get; set; }

    [XmlElement("SMPFAvailability")]
    public AvailabilityDetails SMPFAvailability { get; set; }
}

public class AvailabilityDetails
{
    [XmlElement("AvailabilityDetails")]
    public AvailabilityContent AvailabilityContent { get; set; }
}

public class AvailabilityContent
{
    [XmlElement("AccessLineID")]
    public string AccessLineID { get; set; }

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

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

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

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

    [XmlElement("EstimatedThroughputSpeedAnnexA")]
    public List<EstimatedThroughputSpeedAnnexA> EstimatedThroughputSpeedAnnexA { get; set; }

    [XmlElement("EstimatedThroughputSpeedAnnexA")]
    public List<EstimatedThroughputSpeedAnnexM> EstimatedThroughputSpeedAnnexM { get; set; }

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

}

public class EstimatedSyncSpeed
{
    [XmlElement("AnnexADownstream")]
    public int AnnexADownstream { get; set; }

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

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

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

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

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

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

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

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

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

public class EstimatedThroughputSpeedAnnexA
{
    [XmlElement("ThroughputSpeedType")]
    public ThroughputSpeedType ThroughputSpeedType { get; set; }
}

public class EstimatedThroughputSpeedAnnexM
{
    [XmlElement("ThroughputSpeedType")]
    public ThroughputSpeedType ThroughputSpeedType { get; set; }
}

public class ThroughputSpeedType
{
    [XmlElement("DownstreamThroughputBottom")]
    public double DownstreamThroughputBottom { get; set; }

    [XmlElement("DownstreamThroughputMin")]
    public double DownstreamThroughputMin { get; set; }

    [XmlElement("DownstreamThroughputTop")]
    public double DownstreamThroughputTop { get; set; }

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

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

    [XmlElement("UpstreamThroughputBottom")]
    public double UpstreamThroughputBottom { get; set; }

    [XmlElement("UpstreamThroughputMin")]
    public double UpstreamThroughputMin { get; set; }

    [XmlElement("UpstreamThroughputTop")]
    public double UpstreamThroughputTop { get; set; }
}

public class ExchangeDetails
{
    [XmlElement("CSSExchangeDistrictCode")]
    public string CSSExchangeDistrictCode { get; set; }

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

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

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

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

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

    [XmlElement("ForecastDate")]
    public DateTime ForecastDate { get; set; }
}

public class CircuitLineLength
{
    [XmlElement("Length")]
    public int Length { get; set; }

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

序列化代码:

string Outerxml = xmlToFormat.FirstChild.FirstChild.FirstChild.FirstChild.OuterXml;
        string formatedXml = XmlFormatter.RemoveXmlns(Outerxml);

        Availability result;

        // Deserialises xlm into an object 

        XmlSerializer serializer = new XmlSerializer(typeof(Availability));
        using (TextReader reader = new StringReader(formatedXml))
        {
            result = (Availability)serializer.Deserialize(reader);
        }

        return result;

您有此 XML 名称出现两次:

[XmlElement("EstimatedThroughputSpeedAnnexA")]

我发现它有这个内部异常:

InvalidOperationException: The XML element 'EstimatedThroughputSpeedAnnexA' from namespace '' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element.

(如果您不需要,不使用 XmlElement 属性的一个很好的理由!)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM