Question

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”}

Was it helpful?

Solution

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

OTHER TIPS

Try this

XDocument xdoc = XDocument.Parse(xml);
var filesArray = xdoc.Elements()
    .First()
    .Descendants()
    .Where(x => x.Name == "file")
    .Select(x => x.Value)
    .ToArray();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top