Сериализация xml в объекте c # - PullRequest
0 голосов
/ 18 октября 2019

У меня возникли некоторые проблемы с попыткой сериализации моего 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;

1 Ответ

0 голосов
/ 18 октября 2019

Это имя 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, если вы нене надо!)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...