Small problem in writing to an XML file

fld00d

Member
Joined
May 9, 2009
Messages
5
Programming Experience
Beginner
Hello,

I am working with an XML file that uses Nodes and Attributes. I have written a function to handle the writing of the XML data to a file:

VB.NET:
    Public Function WriteXmlData(ByVal StrNodeKeyName As String, ByVal strNodeVal As String, ByRef XmlFileName As String) As String
        Dim strMessage As String = ""
        Try
            Dim XmlFileWriter As New System.Xml.XmlDocument
            XmlFileWriter.Load(XmlFileName)
            Dim nod As XmlNode = XmlFileWriter.SelectSingleNode("/pageinfo[@key=strNodeKeyName]")
            If nod IsNot Nothing Then
                nod.Attributes("value").Value = strNodeVal
                XmlFileWriter.Save(XmlFileName)
                strMessage = "Meta content updated successfully."
            Else
                strMessage = "Error writing meta content."
            End If
        Catch ex As Exception
            Throw ex
            strMessage = "Error writing meta content."
        End Try
        Return strMessage
    End Function

My code fails on the XmlFileWriter.SelectSingleNode statement. The variable 'nod' always contains nothing, meaning that I am probably not selecting the node/attribute correctly.

I call the function using the following statements:

VB.NET:
            Dim strMessage1 As String = WriteXmlData("title", strDocTitle, strXmlFileName)
            Dim strMessage2 As String = WriteXmlData("description", strDocDescription, strXmlFileName)
            Dim strMessage3 As String = WriteXmlData("keywords", strDocKeywords, strXmlFileName)
            Dim strMessage4 As String = WriteXmlData("content", strDocContent, strXmlFileName)

My XML file format is as follows:

VB.NET:
<?xml version="1.0" encoding="utf-8"?>
<pageinfo>
  <add key="title" value="Some Title" />
  <add key="keywords" value="some keywords" />
  <add key="content" value="some content words" />
  <add key="description" value="a description" />
</pageinfo>

As you might suspect, this XML file is pulled by a webpage and reads the XML data in the VALUE attribute to set the Doc Title and Meta content for it's respective webpage using ASP.Net and VB.Net.

Any help with this would be outstanding, as I know that this is just a limitation of my knowledge with VB.Net and probably a very simple fix that I cannot figure out.
 
You have a xpath expression to select a "pageinfo" node that has a "key" attribute of certain value, but your "pageinfo" node doesn't have any "key" attributes, in fact it doesn't have any attributes at all. Can you see where this is getting at? Select a "add" node instead, the "add" nodes have attributes. I also recommend the Xml and Xpath tutorials at W3Schools Online Web Tutorials for starters.
 
So the line should read something like:

VB.NET:
Dim nod As XmlNode = XmlFileWriter.SelectSingleNode("/pageinfo/add[@key=strNodeKeyName]")

How do I get it to accept the variable strNodeKeyName in this example? It still evals to 'Nothing' in this example. This would work fine if there was a key attribute named 'strNodeKeyName'. I need to use a variable in this instance.




Thanks!
 
Use the & operator to add strings together. & Operator (Visual Basic) You also have to 'quote' the string value to compare in the Xpath expression. I usually use a string variable and String.Format the expression, makes it easier to read and avoids silly typos.
VB.NET:
Dim xpath As String = String.Format("/pageinfo/add[@key='{0}']", strNodeKeyName)
 
Thank you!

That totally worked...

My solution is as follows:
VB.NET:
            Dim strPath As String = String.Format("/pageinfo/add[@key='{0}']", StrNodeKeyName)
            Dim nod As XmlNode = XmlFileWriter.SelectSingleNode(strPath)


Thank you very much!:)
 
Back
Top