【问题标题】:Getting the information of repeated nodes from a xml file in C#从 C# 中的 xml 文件中获取重复节点的信息
【发布时间】:2013-10-10 06:41:08
【问题描述】:

我有一个xml文件如下:

<Root>
  <Folder1>
    <file>AAA</file>
    <file>BBB</file>
    <file>CCC</file> 
  </Folder1>
  <Folder2>
    <file>AAA</file>
    <file>BBB</file> 
    <file>CCC</file> 
  </Folder2>
</Root>

我需要一个字符串列表中的所有父母, 我尝试使用

using (XmlTextReader reader = new XmlTextReader(pathFiles))              
{                    
   reader.ReadToFollowing("file");
   string files = reader.ReadElementContentAsString();
}

所以,“files”变量只包含“AAA”,

reader.ReadElementContentAsString() 不接受列表。

有没有办法将输出提取为{“AAA”,”BBB”,”CCC”, AAA”,”BBB”,”CCC”}

【问题讨论】:

    标签: c# .net xml c#-4.0 c#-3.0


    【解决方案1】:
    XDocument doc=XDocument.Load(xmlPath);
    List<string> values=doc.Descendants("file")
                           .Select(x=>x.Value)
                           .ToList();
    

    【讨论】:

      【解决方案2】:

      试试这个

      XDocument xdoc = XDocument.Parse(xml);
      var filesArray = xdoc.Elements()
          .First()
          .Descendants()
          .Where(x => x.Name == "file")
          .Select(x => x.Value)
          .ToArray();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-18
        • 2021-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-22
        相关资源
        最近更新 更多