Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Query Specific part of XML file using XPathNavigator and XPathDocument
[ All Languages » VB.net »  XML]

Total Hit ( 2148)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


our sample XML document is as below

Click here to copy the following block
<?xml version="1.0" encoding="utf-8" ?>
<SideBar>
   <Panel Caption="Downloads">
       <Link Text="VB" URL="LanguageCategory.aspx?SelectionId=1" />
       <Link Text="VB.net" URL="LanguageCategory.aspx?SelectionId=2" />
       <Link Text="C#" URL="LanguageCategory.aspx?SelectionId=3" />
       <Link Text="ASP" URL="LanguageCategory.aspx?SelectionId=5" />
       <Link Text="ASP.net" URL="LanguageCategory.aspx?SelectionId=4" />
       <Link Text="SQL" URL="LanguageCategory.aspx?SelectionId=6" />
   </Panel>
   <Panel Caption="Projects">
       <Link Text="Textile Automation" URL="Projects.aspx?SelectionId=1" />
   </Panel>
   <Panel Caption="Certifications">
       <Link Text="MCSD.net" URL="Certifications.aspx?SelectionId=1" />
       <Link Text="MCSA" URL="Certifications.aspx?SelectionId=2" />
       <Link Text="MCDBA" URL="Certifications.aspx?SelectionId=3" />
       <Link Text="MCSE" URL="Certifications.aspx?SelectionId=4" />
   </Panel>
   <Panel Caption="Links">
       <Link Text="VB Links" URL="Links.aspx?SelectionId=1" />
       <Link Text="VB.net Links" URL="Links.aspx?SelectionId=2" />
       <Link Text="SQL Links" URL="Links.aspx?SelectionId=3" />
       <Link Text="ASP.net Links" URL="Links.aspx?SelectionId=4" />
   </Panel>
   <Panel Caption="About Me">
       <Link Text="My Job" URL="AboutMe.aspx?SelectionId=1" />
       <Link Text="My Friends" URL="AboutMe.aspx?SelectionId=2" />
       <Link Text="My Family" URL="AboutMe.aspx?SelectionId=3" />
       <Link Text="My Hobby" URL="AboutMe.aspx?SelectionId=4" />
   </Panel>
   <Panel Caption="Photo Galary">
       <Link Text="College Time" URL="PhotoGalary.aspx?SelectionId=1" />
       <Link Text="Richmond Function" URL="PhotoGalary.aspx?SelectionId=2" />
       <Link Text="Family Photos" URL="PhotoGalary.aspx?SelectionId=3" />
       <Link Text="Other" URL="PhotoGalary.aspx?SelectionId=4" />
   </Panel>
</SideBar>

We will use following function to find specific panel data.

Click here to copy the following block
Function ParseXML(ByVal XMLFilePath As String, ByVal PanelCaption As String)
  Dim xDoc As New XPathDocument(XMLFilePath)
  Dim xNav As XPathNavigator = xDoc.CreateNavigator()

   '*********************************************************************************************************
   'XPath Query to find All links under panel having caption="Downloads" is as below
   ' /SideBar/Panel[@Caption="Downloads"]/Link
   '*********************************************************************************************************
  Dim xIterator As XPathNodeIterator = xNav.Select("/SideBar/Panel[@Caption=""" & PanelCaption & """]/Link")

  'Dim dt As New DataTable
  'Dim dr As DataRow
  'dt.Columns.Add ("Text")
  'dt.Columns.Add ("URL")

  While xIterator.MoveNext()
    'Console.WriteLine(xIterator.Current.Value)
    'dr = dt.NewRow()
    'dr.Item(0) = xIterator.Current.GetAttribute("Text", "")
    'dr.Item(1) = xIterator.Current.GetAttribute("URL", "")
    'dt.Rows.Add (dr)

    Console.WriteLine(xIterator.Current.GetAttribute("Text", ""))
    Console.WriteLine(xIterator.Current.GetAttribute("URL", ""))
  End While
  
End Function

we can call this function as following

Click here to copy the following block
ParseXML("C:\MySideBar.xml","Downloads")
ParseXML("C:\MySideBar.xml","AboutMe")
ParseXML("C:\MySideBar.xml","Links")

Happy Programming ....



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.