问题反序列化Xml,任何帮助赞赏! [英] Problem deserialize Xml, any help apprecciated!

查看:90
本文介绍了问题反序列化Xml,任何帮助赞赏!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我无法反序列化Xml。我得到AccountInformation工作,但它不会与Leauges一起工作。 Xml不包含Leauges的任何标记,我不想添加该标记以使其工作。有没有其他方法来修复它?





Hi,
i am having trouble deserializing Xml. I get AccountInformation to work but it wont work with the Leauges. The Xml doesnt contain any tag for "Leauges" and i dont want to add that tag to get it to work. Is there any other way to "fix" it?


[Serializable()]
[XmlRoot(ElementName = "XMLSOCCER.COM", Namespace = "")]
public class XMLSOCCER
{
    [XmlArray("Leauge")]
    [XmlArrayItem("Leauge", typeof(Leauge))]
    public Leauge[] Leauges { get; set; }

    [XmlElement(ElementName = "AccountInformation")]
    public string AccountInformation { get; set; }

}







[Serializable()]
public class Leauge
{
    [XmlElement(ElementName = "Id")]
    public int Id { get; set; }

    [XmlElement(ElementName = "Name")]
    public string Name { get; set; }

    [XmlElement(ElementName = "Country")]
    public string Country { get; set; }

    [XmlElement(ElementName = "Historical_Data")]
    public string Historical_Data { get; set; }

    [XmlElement(ElementName = "Fixtures")]
    public string Fixtures { get; set; }

    [XmlElement(ElementName = "LiveScore")]
    public string Livescore { get; set; }

    [XmlElement(ElementName = "NumberOfMatches")]
    public int NumberOfMatches { get; set; }

    [XmlElement(ElementName = "LatestMatch")]
    public DateTime LatestMatch { get; set; }
}







XML:






XML:

<XMLSOCCER.COM>
  <League>
    <Id>1</Id>
    <Name>English Premier League</Name>
    <Country>England</Country>
    <Historical_Data>Yes</Historical_Data>
    <Fixtures>Yes</Fixtures>
    <Livescore>Yes</Livescore>
    <NumberOfMatches>5700</NumberOfMatches>
    <LatestMatch>2015-05-24T16:00:00+00:00</LatestMatch>
  </League>
  <League>
    <Id>2</Id>
    <Name>English League Championship</Name>
    <Country>England</Country>
    <Historical_Data>Yes</Historical_Data>
    <Fixtures>Yes</Fixtures>
    <Livescore>Yes</Livescore>
    <NumberOfMatches>8372</NumberOfMatches>
    <LatestMatch>2015-05-02T13:15:00+00:00</LatestMatch>
  </League>
  <League>
    <Id>5</Id>
    <Name>Serie A</Name>
    <Country>Italy</Country>
    <Historical_Data>Yes</Historical_Data>
    <Fixtures>Yes</Fixtures>
    <Livescore>Yes</Livescore>
    <NumberOfMatches>5404</NumberOfMatches>
    <LatestMatch>2015-05-31T20:45:00+00:00</LatestMatch>
  </League>
  <AccountInformation>
   Confidential info
  </AccountInformation>
</XMLSOCCER.COM>





反序列化代码:





Deserialize code:

public static void Main(string[] args)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(XMLSOCCER));
            TextReader reader = new StreamReader(@"C:\Xml.xml");
            object obj = deserializer.Deserialize(reader);
            XMLSOCCER XmlData = (XMLSOCCER)obj;
            reader.Close();
        }

推荐答案

问题是您的类声明不符合XML格式...

您的代码期望的是一组联盟节点,这些节点包含在一个联盟节点中以形成一种数组:

The problem is that your class declaration does not fit the XML format...
What your code is expecting is a group of League nodes enclosed in a League node to form a kind of array:

<XMLSOCCER.COM>
  <Leauge>
    <Leauge>
      ...
    </Leauge>
    <Leauge>
      ...
    </Leauge>
  </Leauge>
  ...
</XMLSOCCER.COM>



现在,您可以更改XML的结构以适应声明或更改声明以适应XML ... < br>

XML更改


Now, you can change the structure of your XML to fit the declarations or change the declarations to fit the XML...

XML changes

<XMLSOCCER.COM>
  <League>
    <League>
      <Id>1</Id>
      <Name>English Premier League</Name>
      <Country>England</Country>
      <Historical_Data>Yes</Historical_Data>
      <Fixtures>Yes</Fixtures>
      <Livescore>Yes</Livescore>
      <NumberOfMatches>5700</NumberOfMatches>
      <LatestMatch>2015-05-24T16:00:00+00:00</LatestMatch>
    </League>
    ...
  </League>
  ...
</XMLSOCCER.COM>



代码更改


Code changes

[Serializable()]
[XmlRoot(ElementName = "XMLSOCCER.COM", Namespace = "")]
public class XMLSOCCER
  {
  [XmlElement("Leauge")]
  public Leauge[] Leauges { get; set; }
  [XmlElement(ElementName = "AccountInformation")]
  public string AccountInformation { get; set; }
}


这篇关于问题反序列化Xml,任何帮助赞赏!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆