By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,133 Members | 1,113 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,133 IT Pros & Developers. It's quick & easy.

Vb .Net : Reading / Editing XML file using XPath

P: 2
Hi Guys,

I am new to this forum and I am totally impressed with all of the knowledge. I am need of some help because I have almost pulled all of my hair out. I am trying to create a very simple form that lets a user only select three stories to vote on. When they submit the form I was wanting the VB to update the Count on the correct story for whatever three stories the user selected. Any ideas? The VB below goes through all of the nodes and updates the "Count" attribute, but it does it for all of the values which is of no use to me.

XML Structure
Expand|Select|Wrap|Line Numbers
  1. <votes>
  2. <story Id="1" Count="0"></story>
  3. <story Id="2" Count="0"></story>
  4. <story Id="3" Count="0"></story>
  5. <story Id="4" Count="0"></story>
  6. <story Id="5" Count="0"></story>
  7. </votes>
Expand|Select|Wrap|Line Numbers
  1. Imports System.Xml
  2. Imports System.Text
  3. Imports System.IO
  5. Partial Class submitMe
  6.     Inherits System.Web.UI.Page
  7.    Sub submitMe(sender As Object, ByVal e As System.EventArgs)
  8.        'Connection Strings
  9.        Dim strCurrentPath As String = Request.PhysicalPath
  10.        Dim strXMLPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "\")) & "xml_submissions.xml"
  12.     Dim doc As XmlDocument = New XmlDocument()
  13.          doc.Load(strXMLPath)
  15.        Dim nodeList As XmlNodeList = doc.SelectNodes("//story")
  16.        Dim node As XmlNode
  18.            For Each node In nodeList
  19.             node.Attributes("Count").Value = node.Attributes("Count").Value + 1
  20.            Next
  21.        doc.Save(strXMLPath)
  22.    End Sub
  23. End Class
Any help would be greatly appreciated :-)
Jan 22 '08 #1
Share this Question
Share on Google+
4 Replies

Shashi Sadasivan
Expert 100+
P: 1,435
Hi newGuy001

Welxcome to TSDN. Please follow the Posting guidelines which can be found from the Help link on the upper right corner of the page.

Your thread title is inappropriate.

Jan 22 '08 #2

Shashi Sadasivan
Expert 100+
P: 1,435
HI ,
You cannot directly use XPath to get to your node.
If you do want to use XPath you will have to open the xml file as an XPathDocument
More can be found here How To Query XML with an XPath Expression by Using Visual C# .NET
There should a VB .Net example of the same too, but the idea remais the same.

Jan 22 '08 #3

P: 17

I think if u format ur Xml document it will be easy for you to update the count value.You have given <story id="1" count="0"></story> i.e you have give count and id as attributes to the story node.Instead of that you can give count as a child node to the story node.
Like this way
<story id="1">
This will be preety easy to work it is a good practise too..
You can get the path of the XML doc using Server.Mapath() function..
Hope this is useful to u..
Happy coding
Jan 22 '08 #4

P: 2
Thanks for the help guys! I am trying to get a snippet of code on how to assign a form value to an XML node.
Jan 22 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.