I have following XML file, i want to know best way to read this XML file
<MyFile>
<Companies>
<Company>123</Company>
<Company>456</Company>
<Company>789</Company>
</Companies>
</MyFile>
As an output i need collection of values like "123,456,789" or it could be array of string[]
Can we use Linq to xml? How?
var xdoc = XDocument.Load(PATH_TO_FILE);
var companies = xdoc.Descendants("Company").Select(c => (string)c).ToArray();
This will give you a string[]
.
Use LINQ to XML, Include using System.Xml.Linq;
XDocument xmlDoc = XDocument.Load("yourfile.xml");
var test = xmlDoc.Descendants("Companies").Elements("Company").Select(r => r.Value).ToArray();
string result = string.Join(",", test);
Output would be:
123,456,789
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