The XML dataset file after I add it to a DataSet looks like this:
<?xml version="1.0" standalone="yes"?>
<root>
<Email></Email>
<FirstName></FirstName>
<LastName></LastName>
<Addresses>
<item>
<Address1></Address1>
</item>
<item>
<Address1></Address1>
</item>
</Addresses>
<Friends>
<item>
<Name></Name>
</item>
<item>
<Name></Name>
</item>
</Friends>
</root>
I am having issues accessing the Address1 field or the Name field. I can loop through the Addresses or Friends tables but that doesn't help me since the data I want is wrapped one more level down.
I tried this:
foreach (DataRow ar in ds.Tables["Addresses"].Rows)
{
DataRow[] orderDetails = ar.GetChildRows("item");
}
But no success.
Help appreciated.
Thanks
using linq to XML
public static XDocument GetXDocument()
{
XDocument mydata = XDocument.Parse("<?xml version=\"1.0\" standalone=\"yes\"?><root><Email></Email><FirstName></FirstName><LastName></LastName><Addresses><item><Address1>TestData</Address1></item><item><Address1></Address1></item></Addresses> <Friends> <item> <Name></Name> </item><item><Name></Name></item></Friends></root>");
return mydata;
}
this gets the data as a XDocument and this is how you deal with the data
public void OutputAddress()
{
XDocument data = xmlData.GetXDocument();
string Expected = "TestData";
var result = from
addesses in data.Element("root").Elements("Addresses")
where
addesses.Element("item").Element("Address1").Value != string.Empty
select addesses.Element("item").Element("Address1").Value;
foreach (string address1 in result)
{
Console.Write(address1);
}
}
I suggest you use an object wrapper whenever working with XML. This is very eashy to do, I suggest you have a look at this blog: http://www.picnet.com.au/blogs/Guido/post/2009/09/10/XML-Settings-Files-No-more-webconfig.aspx
Which is aimed at settings XML files however it still applies here.
Thanks
Guido Tapia
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.