XMLNode not saving

SiX

Member
Joined
Dec 23, 2007
Messages
5
Programming Experience
1-3
Hello Guys,

I have following code:

VB.NET:
 Private Sub SetViewHigher(ByVal XMLFile As String)
        Dim XMLDoc As New XmlDocument
        XMLDoc.Load(XMLFile)
        Dim viewtimes As XmlNodeList = XMLDoc.GetElementsByTagName("Viewtimes")
        If viewtimes.Count = 0 Then
        Else
            Dim viewtimer As Integer = viewtimes.Item(0).InnerText
            viewtimer = viewtimer + 1
            Dim XMLDom As New XmlDocument()
            XMLDom.Load(XMLFile)
            Dim Viewnode As XmlNode = XMLDoc.SelectSingleNode("/Movie/Viewtimes")
            Viewnode.InnerText = viewtimer.ToString
            XMLDom.Save(XMLFile)
        End If
    End Sub

My goal is to set the viewtimes in the XML file 1 higher.

When i monitor the viewnode var, it gets updated with 1 more, but when i look at the actual xml file, its not updated.

Any error in my code?

The xml file looks like this:
VB.NET:
  <?xml version="1.0" ?> 
 <!-- Movie: C:\Temp\MC\vorst1
  --> 
 <Movie>
  <Description>None</Description> 
  <PersonalComments>None</PersonalComments> 
  <Viewtimes>10</Viewtimes> 
  </Movie>
 
You got confusion to get rid of. This does it:
VB.NET:
Private Sub SetViewHigher(ByVal XMLFile As String)
    Dim XMLDoc As New Xml.XmlDocument
    XMLDoc.Load(XMLFile)
    Dim viewtimes As Xml.XmlNode = XMLDoc.SelectSingleNode("//Viewtimes")
    If viewtimes IsNot Nothing Then
        viewtimes.InnerText = CInt(viewtimes.InnerText) + 1
        XMLDoc.Save(XMLFile)
    End If
End Sub
 
First of all thx, your code works.

Second, i know your code is much cleaner, but i still cant see why mine doesnt work.

Oh well, im happy it works now, thx.
 
because you didn't save the XmlDocument you modified.

VB.NET:
Dim XMLDom As New XmlDocument()
            XMLDom.Load(XMLFile)
            Dim Viewnode As XmlNode = XMLDoc.SelectSingleNode("/Movie/Viewtimes")
            Viewnode.InnerText = viewtimer.ToString
            XMLDom.Save(XMLFile) [COLOR="Blue"]<-- This will save it no ?[/COLOR]
 
Ya, but you didn't change "XMLDom", you changed "XMLDoc"... :rolleyes: they were two difference XmlDocument instances you opened from same Xml source file.
 
Back
Top