Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

hi guys, im trying to get xml working in a program im making. The xml file im trying to read looks like this:

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<SearcherPrefs>
<Search>
	<name></name>
	<space></space>	
	<urlpart1></urlpart1>
	<urlpart2></urlpart2>
</Search>
<Search>
	<name></name>
	<space></space>	
	<urlpart1></urlpart1>
	<urlpart2></urlpart2>
</Search>
</SearcherPrefs>

 

Im real new to xml so bare with me :)

 

Basicly what i want todo is get the text from name, space, urlpart1 and 2 and store them in a string while i do something with them, then go onto the next <Search> thing and repeat until theres none left.

 

I looked at http://www.xtremedotnettalk.com/showthread.php?s=&threadid=75877 and from that made this code:

 

           Dim name As String
           Dim space As String
           Dim urlpart1 As String
           Dim urlpart2 As String

           Dim xPathDocument As New XPath.XPathDocument("C:\test.xml")
           Dim xPathNav As XPath.XPathNavigator = xPathDocument.CreateNavigator
           Dim xPathNodeIterate As XPath.XPathNodeIterator

           xPathNodeIterate = xPathNav.Select("descendant::Search/name")
           xPathNodeIterate.MoveNext()
           name = xPathNodeIterate.Current.Value

           xPathNodeIterate = xPathNav.Select("descendant::Search/space")
           xPathNodeIterate.MoveNext()
           space = xPathNodeIterate.Current.Value

           xPathNodeIterate = xPathNav.Select("descendant::Search/urlpart1")
           xPathNodeIterate.MoveNext()
           urlpart1 = xPathNodeIterate.Current.Value

           xPathNodeIterate = xPathNav.Select("descendant::Search/urlpart2")
           xPathNodeIterate.MoveNext()
           urlpart2 = xPathNodeIterate.Current.Value

           MsgBox(name)
           MsgBox(space)
           MsgBox(urlpart1)
           MsgBox(urlpart2)

           'now i want to goto the next <Search> tag and repeat

 

The only problem is, that will only work with the first <Search> thing...(what are they called anyway? sorry im real new to xml) So basicly i need to know how to goto the next <Search> thing after im done with the one before and then repeat the code.

 

Any help would be appreciated, ive looked all over the net and cant find anything that seems to help me with what i need.....

 

 

Thanks, Dan

Edited by dannyres
Posted

this should help you pull out all the <Search> elements:

 


Dim xPathDocument As XPathDocument = New XPathDocument(strDocument)

       Dim xPathNav As XPathNavigator = xPathDocument.CreateNavigator

       xPathNav.MoveToRoot()

       Dim xPathNodeIterate As XPathNodeIterator = xPathNav.Select("descendant::Search")

       While xPathNodeIterate.MoveNext

           ' put your code in here to pull out name, space etc
           'xPathNodeIterate.Current.Name
           'xPathNodeIterate.Current.Value 

       End While

My website
Posted (edited)

hmm thats seems to work if i do something like this:

 

       xPathNodeIterate = xPathNav.Select("descendant::Search/name")

       While xPathNodeIterate.MoveNext
           name = xPathNodeIterate.Current.Value
           MsgBox(name)
       End While

       xPathNodeIterate = xPathNav.Select("descendant::Search/space")

       While xPathNodeIterate.MoveNext
           space = xPathNodeIterate.Current.Value
           MsgBox(space)
       End While

'etc etc

 

But what if i want to get the value for each thing... name, space etc etc then do something, then go onto the next set of them?

 

If i use:

 

xPathNodeIterate = xPathNav.Select("descendant::Search")

 

Then it just returns all of the text for each thing... name space etc etc in one string which wont work for what i need...

 

 

Dan

Edited by dannyres
  • 2 months later...
Posted

Try this

 

Dim expr As XPathExpression

expr = myXPathNavigator.Compile("//Search/Name | //Search/space")

Dim myXPathNodeIterator As XPathNodeIterator = myXPathNavigator.Select(expr)

 

While (myXPathNodeIterator.MoveNext())

Console.WriteLine("<" & myXPathNodeIterator.Current.Name & ">" & myXPathNodeIterator.Current.Value)

End While

 

Catch e As Exception

Console.WriteLine("Exception: {0}", e.ToString())

End Try

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...