Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

Shine S
srirama
More...


Forums » .NET » Webservices »

How to read xml node value in c#


Posted Date: 14 Jun 2012      Posted By:: Somasekar N     Member Level: Gold    Member Rank: 305     Points: 3   Responses: 6



I'm using http://www.webservicex.net/country.asmx?op=GetISD webservice. Which returns respons as
<string xmlns="http://www.webserviceX.NET">
<NewDataSet> <Table> <code>91</code> <name>India</name> </Table> <Table> <code>91</code> <name>India</name> </Table> </NewDataSet>
</string>.
Using xmlreader i got values as 91india. How can i get code value only?

Click here to get coupon codes...
Coupon Codes




Responses

#675496    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 14/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Check this sample code to read XML


//Reading the XML file, Xread is a XML reader object
while (xRead.Read())
{
XmlNodeType nodeType = xRead.NodeType;

if (nodeType == XmlNodeType.Element)
{
if ((xRead.Name == "graphic") || (xRead.Name == "inline-graphic"))
{
lintImageCount += 1;
if (xRead.HasAttributes)
{
xRead.MoveToAttribute(0);
strImagename = xRead.Value;
blnOk = FileCheck(strImagename);
if (blnOk == false)
{
break;
}
}
}
}
}

}


Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM



 
#675500    Author: Somasekar N      Member Level: Gold      Member Rank: 305     Date: 14/Jun/2012   Rating: 2 out of 52 out of 5     Points: 1

I got string as response, can't read the element name. When i read out put it was shows err like
System.ArgumentException: Illegal characters in path. Line 27:  XmlReader r = XmlReader.Create(code);
Line 28:while (r.Read())


Click here to get coupon codes...
Coupon Codes



 
#675504    Author: Prasad kulkarni        Member Level: Diamond      Member Rank: 8     Date: 14/Jun/2012   Rating: 2 out of 52 out of 5     Points: 3

To Read XML you can use
- XMLReader
- XMLDocument (DOM)
- XPathNavigator

Now, your xml has root node as "string", which contains child node as "NewDataSet"
Let's read file using DOM


//getElementByTagName returns us NodeList
XMLNodeList Xnlist = document.getElementByTagName("Table")
for each (XmlNode XN in Xnlist.Nodes )
{
//here we get value as 91 only.
MessageBox.Show(XN.ChildsNodes[0].InnerText);
}


hope it helps

Thanks
Koolprasd2003
[DotNetSpider MVM]






 
#675505    Author: Asheej T K        Member Level: Diamond      Member Rank: 2     Date: 14/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

Please check below code, Pass the value 91 to call the below function,


private void PopulateXMLData(string id)
{
try
{
string XMLFilePath = System.Configuration.ConfigurationManager.AppSettings["XMLFileName"];
DataSet ds = new DataSet();
ds.ReadXml(XMLFilePath);

foreach (DataRow dr in ds.Tables[0].Rows)
{
if ((dr["code"].ToString()) == id.ToString())
{
txtCountryName.Text = dr["Name"].ToString().Trim();
//Continue for all fields....
}
}
}
catch (Exception ex)
{
lblMsg.Text = "Error while fetching the record" + ex.Message;
}


Let me know if you are looking something different.


Regards,
Asheej T K
Microsoft MVP[ASP.NET/IIS]
DotNetSpider MVM

Dotnet Galaxy



 
#675653    Author: Somasekar N      Member Level: Gold      Member Rank: 305     Date: 15/Jun/2012   Rating: 2 out of 52 out of 5     Points: 1

Thank you all for your response. And using XmlDocument i got the answer.

Click here to get coupon codes...
Coupon Codes



 
#675813    Author: Prachi Kulkarni        Member Level: Gold      Member Rank: 25     Date: 16/Jun/2012   Rating: 2 out of 52 out of 5     Points: 1

Hi,
Thanks.. it solved my problem of reading xml as well.

Regards,
Prachi.



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Fedex Integration in asp.net application
Previous : Call webservice method with dictionary as paramiter
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.