如何在c#中使用linq从具有相同和不同节点的xml中获取相同的元素值

时间:2017-05-24 11:21:48

标签: c# xml linq

如xml文件中所示,元素具有相同和不同的节点,在同一元素A和B中默认为每个如何根据各个元素获取所有节点子节点值

是否可以为它创建一个单独的方法,然后根据类型进行检查,然后获取子节点值?

Xml文件:

 <?xml version="1.0" encoding="utf-8" ?>
    <File>
      <Record>
        <Data>
          <Type>A</Type>
          <office>
            <Road>
              <code> plot 309</code>
            </Road>
          </office>
          <Area>
            <AreaId>Pune</AreaId>
          </Area>
        </Data>
        <Data>
          <Type>B</Type>
          <office>
            <Road>
              <code> plot 309</code>
            </Road>
          </office>
          <Area>
            <AreaId>A50</AreaId>
            <AreaName>Pune</AreaName>
            <AreaDetails>Pune India</AreaDetails>
          </Area>
        </Data>
      </Record>
      <Record>
        <Data>
          <Type>A</Type>
          <office>
            <Road>
              <code> plot 400</code>
            </Road>
          </office>
          <Area>
            <AreaId>Mumbai</AreaId>
          </Area>
        </Data>
        <Data>
          <Type>B</Type>
          <office>
            <Road>
              <code> plot 400</code>
            </Road>
          </office>
          <Area>
            <AreaId>A70</AreaId>
            <AreaName>Mumbai</AreaName>
            <AreaDetails>Mumbai-India</AreaDetails>
          </Area>
        </Data>
      </Record>
    </File>

C#代码:

XDocument xdocTest = XDocument.Load(@"E:xml\XMLFile1.xml");
var testRecords = (from root in xdocTest.Descendants("File")
               from Record in root.Elements("Record")
               select new
               {
                   typeA = (Record.Elements("Data").Elements("Type").Any() ==true) ? Record.Element("Data").Element("Type").Value: string.Empty,
                   typeB = (Record.Elements("Data").Elements("Type").Any() == true) ? Record.Element("Data").Element("Type").Value : string.Empty
                // Remaining child node 
               }).ToList();

2 个答案:

答案 0 :(得分:0)

此代码将返回“Record”列表,其中包含相应的“A”和“B”类型元素:

var testRecords = (from root in xdocTest.Descendants("File")
                    from Record in root.Elements("Record")
                    select new
                    {
                        typeA = (Record.Elements("Data").Elements("Type").Any() == true) ? Record.Elements("Data").Where(x=> x.Element("Type").Value=="A").ToList(): new List<XElement>(),
                        typeB = (Record.Elements("Data").Elements("Type").Any() == true) ? Record.Elements("Data").Where(x => x.Element("Type").Value == "B").ToList() : new List<XElement>()
                        // Remaining child node 
                    }).ToList();

答案 1 :(得分:0)

使用字典:

            XDocument xdocTest = XDocument.Load(@"E:xml\XMLFile1.xml");
            var testRecords = xdocTest.Descendants("Record").Select(x => new Dictionary<string, XElement>(
                x.Elements("Data").GroupBy(y => (string)y.Element("Type"), z => z)
                    .ToDictionary(y => y.Key, z => z.FirstOrDefault())
            ).ToList());