Insert into XML (modify)

justputitdown

Member
Joined
Aug 6, 2006
Messages
7
Programming Experience
Beginner
Ive been reading from and xml file and ive also been able to add new data to it but im having trouble modifying the existing data. Below is the code im trying to use. Its not working and im sure its obvious as to why but im a little stuck... Any ideas? No errors are thrown so as far as i can see the data isnt being saved (ie changing the nodes in the nodelist isnt affecting the xml document)... So very stuck though.

VB.NET:
        Try
            Dim m_xmld As XmlDocument
            Dim m_nodelist As XmlNodeList
            Dim m_node As XmlNode
            m_xmld = New XmlDocument
            m_xmld.Load("E:\Domains\www.wanderingshaman.com\data.xml")
            m_nodelist = m_xmld.SelectNodes("/twsusers/user")
            For Each m_node In m_nodelist
                Dim idXMLValue = m_node.ChildNodes.Item(0).InnerText
                If CInt(idXMLValue) = m_id Then
                    m_node.ChildNodes.Item(1).InnerText = fnameIn
                    m_node.ChildNodes.Item(2).InnerText = lnameIn
                    m_node.ChildNodes.Item(3).InnerText = usernameIn
                    m_node.ChildNodes.Item(4).InnerText = passwordIn
                    m_node.ChildNodes.Item(5).InnerText = emailIn
                    m_node.ChildNodes.Item(6).InnerText = phoneIn
                    m_node.ChildNodes.Item(7).InnerText = newsletterIn
                    m_node.ChildNodes.Item(8).InnerText = adminIn
                    m_node.ChildNodes.Item(9).InnerText = vegetarianIn
                End If
            Next
            m_xmld.Save("E:\Domains\www.wanderingshaman.com\data.xml")
        Catch errorVariable As Exception
            'Error trapping
            Console.Write(errorVariable.ToString())
        End Try
 
Have you debugged, stepping through the code checking the execution path and variables along the way?
What is m_id?
From what I can see the problem is probably that this statement:
VB.NET:
If CInt(idXMLValue) = m_id Then
never evaluates to true.
 
Maybe i'm way off here, but using XML as a datasource is a great idea but why modify it directly. A dataset can read and write XML so why not load it into a dataset modify it there then write it back again. Much more simple.
 
Everytime I suggest using the dataset I'm told it's overkill.
It's the way I do it though.
VB is designed for RAD (Rapid Application Development) which is rad (slang, Excellent; wonderful).
 
Dataset

the m_ID was a variable set earlier used to find the record i wanted to edit... however the dataset idea sounds a lot better.... How does it work?
 
To read and write XML from a dataset is as easy as it sounds....


VB.NET:
DataSet.ReadXml(..)
 
Dataset.WriteXml(..)

There are also methods to just 'infer' read or write the schema. Really usfull i think?:)
 
Obvious...

Cheers... That last post makes it look horrifyingly easy and yet makes me feel a little retarded for not realising :) Cheers for the help i will give it a shot.
 
Also worthy to note that the System.Data.DataTable class has the same methods:
DataTable.ReadXml - 4 overloads
DataTable.WriteXml - 16 overloads

This means that if you have only 1 table defined in the XML datasource, you can use a DataTable instead of a DataSet which will remove one level from your code.
 
Back
Top