IT Community - Software Programming, Web Development and Technical Support

How to Read xml nodes using XPathNavigator?

This is a discussion on How to Read xml nodes using XPathNavigator? within the C# Programming forums, part of the Software Development category; Hi, I have the xml file like this. <Persons> <Person> <Name>xxx</Name&...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 02-08-2008, 02:21 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default How to Read xml nodes using XPathNavigator?

Hi,

I have the xml file like this.

<Persons>
<Person>
<Name>xxx</Name>
<Age>25</Age>
</Person>
<Person>
<Name>yyy</Name>
<Age>25</Age>
</Person>
</Persons>


I want to read this xml file using XPathNavigator. Can anyone give me the sample code to do this.
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-08-2008, 02:26 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: How to Read xml nodes using XPathNavigator?

HI,

This is the sample to do this. Just include the following namespace.

using System.Xml.XPath;


string xmlPath = Application.StartupPath + @"\Persons.xml";
XPathDocument doc = new XPathDocument(xmlPath);
XPathNavigator nav = doc.CreateNavigator();
XPathExpression Expr = nav.Compile("Persons/Person");
XPathNodeIterator iterator = nav.Select(Expr);

XPathExpression nameExpr = nav.Compile("Name");

XPathExpression ageExpr = nav.Compile("Age");

while (iterator.MoveNext())
{
XPathNavigator nameNav = iterator.Current.SelectSingleNode(nameExpr).Clone( );
XPathNavigator ageNav = iterator.Current.SelectSingleNode(ageExpr).Clone() ;
string name = nameNav.Value;
int age = Convert.ToInt32(ageNav.Value);
}
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-08-2008, 04:40 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Smile Re: How to Read xml nodes using XPathNavigator?

Hi,

This post is very useful for me.. I tried above method, Its working fine..
But i need to read xml without using XPath Navigator.... and xmltextreader...

Is anyother method is there to read xml file rather than which i mentioned above
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-22-2008, 10:29 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: How to Read xml nodes using XPathNavigator?

Hi,

Hope this helps.

XML file:

<India>
<State>Tamil Nadu</State>
<State>Andhra Pradesh</State>
<State>Karnataka</State>
<State>Maharastira</State>
<State>Kerala</State>
<State>West Bengal</State>
<State>Misoram</State>
<State>Kashmir</State>
</India>

public string ReadXML(string xmlUrl)
{
System.Text.StringBuilder sbXML = new System.Text.StringBuilder();
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(xmlUrl);

object oProvinceName = reader.NameTable.Add("State");

while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name.Equals(oProvinceName))
{
sbXML.Append(reader.ReadString()).Append("<br />");
}
}
}
return sbXML.ToString();
}
catch (Exception e)
{
Response.Write(e.Message);
return "";
}
finally
{

if (reader != null)
reader.Close();
}
}
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/c-programming/5212-how-read-xml-nodes-using-xpathnavigator.html
Posted By For Type Date
DiscussWeb IT Community - Technical Support and Technology Discussions This thread Refback 02-08-2008 04:34 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to generate nodes of XML in the required format? poornima ASP and ASP.NET Programming 2 02-14-2008 09:17 PM
How to read the text from a .pdf file? vadivelanvaidyanathan Software Testing 0 01-28-2008 10:15 PM
Read XML by Dataset gp_logesh ASP and ASP.NET Programming 0 10-31-2007 03:13 AM
How to Read and write txt file using C# kingmaker C# Programming 2 08-25-2007 02:40 AM
Can anybody tell me how to read XML from Javascript kingmaker HTML, CSS and Javascript Coding Techniques 1 07-23-2007 07:34 AM


All times are GMT -7. The time now is 07:59 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0