簡體   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