簡體   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