繁体   English   中英

从C#中的xml文件获取重复节点的信息

[英]Getting the information of repeated nodes from a xml file in C#

我有一个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();
}

因此,“文件”变量仅包含“ AAA”,

reader.ReadElementContentAsString()不接受List。

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

XDocument doc=XDocument.Load(xmlPath);
List<string> values=doc.Descendants("file")
                       .Select(x=>x.Value)
                       .ToList();

尝试这个

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

暂无
暂无

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

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