简体   繁体   English

解析xml子节点

[英]parse xml children nodes

What is the best way to parse XML children nodes into a specific list? 将XML子节点解析为特定列表的最佳方法是什么? This is a small example of the XML. 这是XML的一个小例子。

<Area Name="Grey Bathroom" IntegrationID="3" OccupancyGroupAssignedToID="141">
  <Outputs>
  <Output Name="Light/Exhaust Fan" IntegrationID="46" OutputType="NON_DIM" Wattage="0" /> 
  </Outputs>
</Area>

I want to create a list or something that will be called the Area Name and hold the information of the Output Name and IntegrationID. 我想创建一个列表或称为“区域名称”的东西,并保存“输出名称”和“ IntegrationID”的信息。 So I can call the list and pull out the Output Name and IntegrationID. 因此,我可以调用列表并提取输出名称和IntegrationID。

I can create a list of all Area Names and then a list of Outputs but cannot figure out how to create a list that will be called "Grey Bathroom" and hold the output "Light/Exhaust Fan" with an ID of 46. 我可以创建所有区域名称的列表,然后创建输出的列表,但无法弄清楚如何创建一个名为“灰色浴室”的列表,并保留ID为46的输出“轻/排气扇”。

XDocument doc = XDocument.Load(@"E:\a\b.xml");
        List<Area> result = new List<Area>();
        foreach (var item in doc.Elements("Area"))
        {
            var tmp = new Area();
            tmp.Name = item.Attribute("Name").Value;
            tmp.IntegrationID = int.Parse(item.Attribute("IntegrationID").Value);
            tmp.OccupancyGroupAssignedToID = int.Parse(item.Attribute("OccupancyGroupAssignedToID").Value);

            foreach (var bitem in item.Elements("Outputs"))
            {
                foreach (var citem in bitem.Elements("Output"))
                {
                    tmp.Outputs.Add(new Output
                    {
                        IntegrationID = int.Parse(citem.Attribute("IntegrationID").Value),
                        Name = citem.Attribute("Name").Value,
                        OutputType = citem.Attribute("OutputType").Value,
                        Wattage = int.Parse(citem.Attribute("Wattage").Value)
                    });
                }
            }
            result.Add(tmp);
        }

public class Area
{
    public String Name { get; set; }
    public int IntegrationID { get; set; }
    public int OccupancyGroupAssignedToID { get; set; }
    public List<Output> Outputs = new List<Output>();
}

public class Output
{
    public String Name { get; set; }
    public int IntegrationID { get; set; }
    public String OutputType { get; set; }
    public int Wattage { get; set; }
}

The example uses an anonymous type. 该示例使用匿名类型。 You could (and I warmly advice you to) use your own. 您可以(我热烈建议您)使用自己的。

var doc = XDocument.Parse(xml);
var areaLists = doc.Elements("Area").
                    Select(e => e.Descendants("Output").
                        Select(d => new
                        {
                            Name = (string) d.Attribute("Name"),
                            Id = (int) d.Attribute("IntegrationID")
                        }).
                        ToArray()).
                    ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM