I've an XML file, in which I'm saving temporary data(index & name of columns) as given below:
-<NewDataSet>
-<USERROWCOL>
<COL>2</COL>
<Name>Name</Name>
</USERROWCOL>
-<USERROWCOL>
<COL>8</COL>
<Name>PDC</Name>
</USERROWCOL>
<NewDataSet>
I want to read all COL
s in an array using C# like it will be an array( {2, 8}
). Can anyone help me about this?
Here is a LINQ to XML Version:
string[] arr = XDocument.Load(@"C:\xxx.xml").Descendants("Name")
.Select(element => element.Value).ToArray();
This will give all the Name
element from the document.
LINQ to XML makes this very easy:
var document = XDocument.Load("file.xml");
var array = document.Descendants("COL").Select(x => (int) x).ToArray();
That's assuming you just want every COL
element in the document, and every element's value will be an integer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With