0

我需要从这个 XML 文件中获取数据,我认为反序列化将是可行的方法,但是,我不知道如何使用 .NET 来做到这一点。

<consoles>
    <console name ="snes">
        <year>1991</year>
        <manufacturer>Nintendo</manufacturer>
    </console>
    <console name = "wii">
        <year>2006</year>
        <manufacturer>Nintendo</manufacturer>
    </console>
    <console name = "ps3">
        <year>2006</year>
        <manufacturer>Sony</manufacturer>
    </console>
</consoles>

基本上,我希望能够随意获取每个控制台的年份或制造商数据。

4

2 回答 2

2
XmlSerializer ser = new XmlSerializer(typeof(console[]),new XmlRootAttribute("consoles"));
var consoles = (console[])ser.Deserialize(stream);


public class console
{
    [XmlAttribute]
    public string name;
    public int year;
    public string manufacturer;
}
于 2012-12-09T22:52:43.153 回答
0
  XDocument doc= XDocument.Load(pathToXmlFilename);

  foreach(XElement element in doc.Root.Elements("console"))
  {
     Console.WriteLine(element.Element("year").Value);
  }
于 2012-12-09T22:41:52.847 回答