Reputation: 990
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”}
Upvotes: 1
Views: 858
Reputation: 73472
Try this
XDocument xdoc = XDocument.Parse(xml);
var filesArray = xdoc.Elements()
.First()
.Descendants()
.Where(x => x.Name == "file")
.Select(x => x.Value)
.ToArray();
Upvotes: 2
Reputation: 32797
XDocument doc=XDocument.Load(xmlPath);
List<string> values=doc.Descendants("file")
.Select(x=>x.Value)
.ToList();
Upvotes: 4