CnuVas CnuVas - 2 months ago 12
C# Question

Convert following String XML into List<string[]>

I have following string XML

<Abc>
<a>value1</a>
<b>value2</b>
<c />
<d />
<x>value3</x>
<y>value4</y>
<z />
</Abc>


Above XML String Convert to
List<string[]>


string[]
have node & value


Output list Should like this


enter image description here

Answer

You can easily extract the list of all child elements using XDocument.Descendants() and use those to create string arrays, containing the element name and value. Wrap it in a list and you're done:

var result = XDocument.Parse(xml)
                      .Descendants()
                      .Select(e => new [] { e.Name.LocalName, e.Value })
                      .ToList();
Comments