将XML的一部分解析为不同的类

时间:2016-11-02 19:54:52

标签: c# xml linq

我试图找出如何将XML文件的一部分读入类中。

这是XML文件的结构:

 <Root>
  <Element1>
    <Element2>
      <ElementID></ElementId>
      <Element3a>
        <Element3aId id="">0</Element3aId>
        <Element3aId id="">0</Element3aId>
      </Element3a>
      <Element3b>
        <Element3bId id="">0</Element3bId>
        <Element3bId id="">0</Element3bId>
      </Element3b>
      <Element3c>
        <Element3cId id="">0</Element3cId>
        <Element3cId id="">0</Element3cId>
      </Element3c>
    </Element2>   
  </Element1>
</Root>

我创建了以下类(我删除了访问器/ mutators以尝试简化代码:

public struct Element3a
{
    [XmlAttribute("id")]
    string id;

    [XmlElement("Element3aId")]
    int Element3aId;
}

[XmlRootAttribute("Element2")]
public struct Element1
{
    [XmlElement("ElementID")]
    int id;

    List<Element3a> Element3aId;

    List<Element3a> Element3bId;

    List<Element3a> Element3cId;
}

这是我的解析代码:

XDocument xDoc = XDocument.Load(filePath);

var Element2List = xDoc.Root.Descendants().FirstOrDefault(c => c.Name.LocalName.Equals("Element1"));
XmlSerializer serializer = new XmlSerializer(typeof(Element1));

using (var reader = Element2List.CreateReader())
{
    do
    {
        reader.ReadToDescendant("Element2");
        Element1 tag1 = (Element1)serializer.Deserialize(reader.ReadSubtree());

        // How do I populate the Element3a Lists here?

    } while (reader.ReadToNextSibling("Station"));
    reader.Close();
}

Element3a / 3b / 3c中都包含相同的数据,因此我希望能够将它们序列化为Element3a结构。我能够将Element2信息放入Element2类中,但我无法在该类中填充Element3a列表。

感谢先进的帮助!我为标签名称道歉,我尽可能地尽量简化。

1 个答案:

答案 0 :(得分:-1)

而不是序列化使用Xml Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Element1 elements = doc.Descendants("Element2").Select(x => new Element1() {
                id = (string)x.Element("ElementID") == "" ? null : (int?)x.Element("ElementID"),
                Element3aId = x.Descendants("Element3a").Select(y => new Element3a {
                    id =  (string)y.Attribute("id"),
                    Element3aId = (string)y.Element("Element3aId") == "" ? null : (int?)y.Element("Element3aId")
                }).ToList(),
                Element3bId = x.Descendants("Element3b").Select(y => new Element3a {
                    id =  (string)y.Attribute("id"),
                    Element3aId = (string)y.Element("Element3bId") == "" ? null : (int?)y.Element("Element3bId")
                }).ToList(),
                Element3cId = x.Descendants("Element3c").Select(y => new Element3a
                {
                    id = (string)y.Attribute("id"),
                    Element3aId = (string)y.Element("Element3cId") == "" ? null : (int?)y.Element("Element3cId")
                }).ToList()
            }).FirstOrDefault();
        }
    }
    public struct Element3a
    {
        public string id;
        public int? Element3aId;
    }

    public struct Element1
    {
        public int? id;

        public List<Element3a> Element3aId;

        public List<Element3a> Element3bId;

        public List<Element3a> Element3cId;
    }
}