【问题标题】:Serialize xml to c# object将xml序列化为c#对象
【发布时间】:2019-10-18 11:34:33
【问题描述】:

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

错误:

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

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

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

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

我查看了内部异常,但不存在更多细节。我确信这与我的对象的设置方式有关。

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>

型号:

[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 并查看与示例 xml 的差异。
  • 也许你需要所有类的 [Serializable] 属性

标签: c# xml soap


【解决方案1】:

这个 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 属性!)

【讨论】:

    猜你喜欢
    • 2014-11-30
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2018-10-11
    • 2011-05-06
    相关资源
    最近更新 更多