简体   繁体   中英

Serialize xml to c# object

I have been having a few problems with trying to serialize my XML to a C# object. I am getting multiple "System.InvalidOperationException".

Errors:

  • InvalidOperationException: There was an error reflecting property 'MPFAvailability'.

  • InvalidOperationException: There was an error reflecting type 'TalkTalkAPI.Models.AvailabilityDetails'.

  • InvalidOperationException: There was an error reflecting property 'AvailabilityContent'.

  • InvalidOperationException: There was an error reflecting type 'TalkTalkAPI.Models.AvailabilityContent'.

I have looked within the inner exception but no further details exist. I am sure this has something to do with the way my object is set up.

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; }
}

Serializer code:

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;

You have this XML name appearing twice:

[XmlElement("EstimatedThroughputSpeedAnnexA")]

I found it with this inner exception:

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.

(a good reason not to use XmlElement attributes if you don't need to!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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