views:

56

answers:

2

The xml dataset file after I add it to a dataset 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

A: 

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

gatapia
+1  A: 

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);
    }
}
Bob The Janitor