【问题标题】:How to get same element value from xml which having same and different nodes using linq in c#如何在c#中使用linq从具有相同和不同节点的xml中获取相同的元素值
【发布时间】:2017-05-24 11:21:47
【问题描述】:

如 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();

【问题讨论】:

  • 这样使用好不好: typeA = (Record.Elements("Data").First().Elements("Type").Any() ==true) ? Record.Elements("Data").First().Element("Type").Value: string.Empty, typeB = (Record.Elements("Data").Last().Elements("Type").Any () == 真) ? Record.Elements("Data").Last().Element("Type").Value : string.Empty

标签: c# xml linq


【解决方案1】:

此代码将返回“记录”列表及其各自的“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();

【讨论】:

  • 我想要这样的值:typeA = 'A' and typeB = 'B' and code for type A as codeA = 'plot 309' and code type for B as codeB = 'plot 309' 所以最后我会得到所有节点的列表,所以我可以直接使用它(注意:像这样我可以得到剩余的节点)
  • @yashfale 为什么不为 Data 创建类并只反序列化 xml?
【解决方案2】:

使用字典:

            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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多