I have an XML file like this and I want to read the value of ID, shortname, Name node.
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <AccountingUnitList xmlns="http://www.google.com"> <AccountingUnit> <ID>17406</ID> <ShortName>test</ShortName> <Name>test</Name> </AccountingUnit> <AccountingUnit> <ID>18006</ID> <ShortName>ANOTHERtest</ShortName> <Name>Anothertest</Name> </AccountingUnit> <AccountingUnit> <ID>18046</ID> <ShortName>RKU</ShortName> <Name>hospital</Name> </AccountingUnit> <AccountingUnit> <ID>18047</ID> <ShortName>MNU</ShortName> <Name>MNU</Name> </AccountingUnit> </AccountingUnitList>
what is the best way to read a node element recursively?
This is how I try to read the value of node:
var accountingunit = ( from e in XDocument.Parse(textresult).Root.Elements("AccountingUnit") select new node { idvalue = (string)e.Element("ID"), shortname =(string)e.Element("ShortName"), name = (string)e.Element("Name"), }); foreach(var unit in accountingunit) { Console.WriteLine("ID"+ unit.idvalue + unit.name + unit.shortname); }
Here is the node constructor:
public class node { public string idvalue { get; set; } public string shortname { get; set; } public string name { get; set; } }
source share