XDocument doc=XDocument.Load(xmlPath);
List<string> values=doc.Descendants("file")
.Select(x=>x.Value)
.ToList();
Getting the information of repeated nodes from a xml file in C#
문제
I have a xml file as follows:
<Root>
<Folder1>
<file>AAA</file>
<file>BBB</file>
<file>CCC</file>
</Folder1>
<Folder2>
<file>AAA</file>
<file>BBB</file>
<file>CCC</file>
</Folder2>
</Root>
I need all the parents in a list of string, I tried using
using (XmlTextReader reader = new XmlTextReader(pathFiles))
{
reader.ReadToFollowing("file");
string files = reader.ReadElementContentAsString();
}
So, "files" variable contains only “AAA” ,
reader.ReadElementContentAsString()
doesn’t accept List.
Is there any way to extract the output as {“AAA”,”BBB”,”CCC”, AAA”,”BBB”,”CCC”}
해결책
다른 팁
Try this
XDocument xdoc = XDocument.Parse(xml);
var filesArray = xdoc.Elements()
.First()
.Descendants()
.Where(x => x.Name == "file")
.Select(x => x.Value)
.ToArray();
제휴하지 않습니다 StackOverflow