【问题标题】:Deserialize XML using XmlSerializer使用 XmlSerializer 反序列化 XML
【发布时间】:2012-10-18 19:07:56
【问题描述】:

我有以下 XML

<?xml version="1.0" ?>
<SERVICES.OUTPUTResponse>
  <Results>
    <Result>
      <Dataset name="OutputData">
        <Row>
            <country>USA</country>
            <pubyear>9986</pubyear>       
            <numart>123</numart>
            <numcites>456</numcites>
        </Row>
        <Row>
            <country>USA</country>
            <pubyear>97</pubyear>
            <numart>895</numart>
            <numcites>231</numcites>
        </Row>
      </Dataset>
      <Dataset name="Result 2">
        <Row>
          <Result_2>
            true
          </Result_2>
        </Row>      
      </Dataset>
    </Result>
  </Results>
  <_Probe></_Probe>
</SERVICES.OUTPUTResponse>

我尝试使用 XmlSerializer 反序列化,但它返回 null。

我使用的属性类是

公共类 XMLDetails {

    public string country { get; set; }

    public string pubyear { get; set; }

    public string numart { get; set; }

    public string numcites { get; set; }
}

反序列化代码是

XmlRootAttribute xRoot = new XmlRootAttribute();
                xRoot.ElementName = "SERVICES.OUTPUTResponse";   
                                xRoot.IsNullable = true;
                var serializer = new XmlSerializer(typeof(XMLDetails), xRoot);
                var reader = new StringReader(remoteXml);
var objpublication = (XMLDetails)(serializer.Deserialize(reader));

请帮我重新爱它

【问题讨论】:

  • 是否需要反序列化为 XMLDetails 对象?

标签: c# xml-parsing


【解决方案1】:

如果你想使用 Linq To Xml

XDocument xDoc = XDocument.Parse(xml); //or XDocument.Load(filename);

var rows = xDoc.XPathSelectElement("//Dataset[@name='OutputData']")
            .Descendants("Row")
            .Select(r => new XMLDetails
            {
                country = r.Element("country").Value,
                pubyear = r.Element("pubyear").Value,
                numart = r.Element("numart").Value,
                numcites = r.Element("numcites").Value,
            })
            .ToList();

PS:必需的命名空间System.Xml.LinqSystem.Xml.XPath

【讨论】:

    【解决方案2】:

    首先您需要使用xsd.exe 生成.xsd(模式)文件和.cs(类)文件

    XML Schema Definition Tool (Xsd.exe)

    您可以运行“Visual Studio 命令提示符”,xsd.exe 路径定义已经定义好,可以使用了。

    在控制台中输入以下命令 *我假设您的 xml 保存在 "yourxmlfile.xml"

    &gt;xsd.exe yourxmlfile.xml

    此命令将生成 "yourxmlfile.xsd" 文件

    然后执行以下命令生成.cs文件 但在改变之前

    &lt;xs:element name="Results" minOccurs="0" maxOccurs="unbounded"&gt;&lt;xs:element name="Results" minOccurs="0" maxOccurs="1"&gt; 在生成的 xsd 文件中 (将结果更改为属性而不是数组属性)

    &gt;xsd.exe yourxmlfile.xsd /c

    此命令将生成 "yourxmlfile.cs"

    现在您可以将此文件添加到您的项目中,您可以如下反序列化 xml 文件

            var serializer = new XmlSerializer(typeof(SERVICESOUTPUTResponse));
            SERVICESOUTPUTResponse instance = null;
            using (var fileStream = File.OpenRead(@"c:\path_to\yourxmlfile.xml"))
            {
                instance = (SERVICESOUTPUTResponse)serializer.Deserialize(fileStream);
            }
    

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 1970-01-01
      • 2020-12-09
      • 2015-09-12
      • 1970-01-01
      • 2018-06-14
      • 2013-04-26
      • 1970-01-01
      相关资源
      最近更新 更多