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&...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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... |
| Sponsored Links |
| |||
| 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 |
| |||
| 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 |
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |