all groups > dotnet xml > september 2004 >
You're in the

dotnet xml

group:

Read complex XML into a dataset or XMLDeSerializer


Read complex XML into a dataset or XMLDeSerializer Shailendra Batham
9/27/2004 11:25:36 AM
dotnet xml:
hey xml gurus
I have a xml which is as follows, so my question is how can I read this
XML is VB.Net or C#. I want to loop for every survey ID and insider
every survey I want to loop for each user ID and get their values.

<?xml version="1.0" encoding="utf-8"?>
<UserData>
<Survey ID="S1">
<User ID="U1">
<EmailAddress>u1@ss.com</EmailAddress>
</User>
<User ID="U2">
<EmailAddress>u2@ss.com</EmailAddress>
</User>
</Survey>
<Survey ID="S2">
<User ID="U4">
<EmailAddress>u4@ss.com</EmailAddress>
</User>
</Survey>
</UserData>

thanks,
shailendra

*** Sent via Developersdex http://www.developersdex.com ***
Re: Read complex XML into a dataset or XMLDeSerializer Victor Hadianto
9/28/2004 10:10:48 AM
Well there are many ways of doing this, you can use XmlReader to read the
XML doc or you can use XPath to get the nodes that you want. Depending on
the your situation you should choose the one that suit you best.

XPath is the most convenient - imho, so I would do:

XmlNodeList surveyList = xmlDoc.SelectNodes("//UserData/Survey");
foreach (XmlNode surveyNode in surveyList)
{
Console.WriteLine(surveyNode.Attributes["ID"].InnerText);
XmlNodeList userList = surveyNode.SelectNodes("User");
foreach (XmlNode userNode in userList)
{
Console.WriteLine(" " + userNode.Attributes["ID"].InnerText);
}
}

---
Victor Hadianto
http://www.synop.com/Products/SauceReader/

[quoted text, click to view]

AddThis Social Bookmark Button