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

本文关键字:分解 XML | 更新日期: 2023-09-27 18:16:13

我想知道如何将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>

我创建了以下类(为了简化代码,我删除了访问器/mutator):

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/Element3b/Element3c都具有相同的数据,因此我希望能够将它们序列化为Element3a结构体。我能够将Element2信息放入Element2类,但我无法在该类中填充Element3a列表。

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

使用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;
    }
}