Appending Element to XML Document

Basic sample class...
Code:
Public Class Member
    Private _Name As String
    Private _Phone As String
    Private _Address As String

    Public Sub New()

    End Sub

    Public Sub New(ByVal name As String, ByVal phone As String, ByVal address As String)
        _Name = name
        _Phone = phone
        _Address = address
    End Sub

    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
    Public Property Phone() As String
        Get
            Return _Phone
        End Get
        Set(ByVal value As String)
            _Phone = value
        End Set
    End Property
    Public Property Address() As String
        Get
            Return _Address
        End Get
        Set(ByVal value As String)
            _Address = value
        End Set
    End Property
End Class

Sample code to populate / save list...
Code:
Dim members As New List(Of Member) for .net 2 use an array or arraylist for .net 1

        members.Add(New Member("Jim Bob", "(123) 456-7890", "Not a cowboy"))
        members.Add(New Member("John Hancock", "(098) 765-4321", "Sometown"))
        members.Add(New Member("etc.", "etc.", "etc."))

        Dim xs As New Xml.Serialization.XmlSerializer(GetType(List(Of Member)))
        Dim fs As New System.IO.FileStream("c:\test.xml", IO.FileMode.Create, IO.FileAccess.ReadWrite)

        xs.Serialize(fs, members)
        fs.Close()

Reading it back in...
Code:
        Dim xs2 As New Xml.Serialization.XmlSerializer(GetType(List(Of Member)))
        Dim fs2 As New System.IO.FileStream("c:\test.xml", IO.FileMode.Open, IO.FileAccess.Read)

        Dim m As List(Of Member)
        m = DirectCast(xs2.Deserialize(fs2), List(Of Member))
        fs2.Close()

hope that gets you started.
 
Wow,

It sounds complicated but I need it and if its the only way to get it done then I am up to learning how....

vbMarkO
 
You must have posted that near the same time I was posting earlier...

Again WOW thats going to take some breaking down .... I will certainly dive into this to try and understand it so I can recreate it later if needed.

However while you were writing this out, I went to figuring about how I did the save and also the remove and it hit me I could remove and save a newrecord at the same time simulating an update of sorts...

Crude but it works :)

I still cant help but feel if I can search and find the exact node to remove I should be able to update more efficiently than this....


Here is my crude method of updating this small addressbook

Code:
  Dim xmlDoc As New XmlDocument
        xmlDoc.Load(xmlFile)

        For Each myNode As XmlNode In xmlDoc.DocumentElement.ChildNodes
            If myNode.FirstChild.InnerText = lstNames.SelectedItem Then
                myNode.ParentNode.RemoveChild(myNode)
                Exit For
            End If
        Next

        xmlDoc.Save(xmlFile)

        /////////////////////////////////

       
         create a new node
        Dim memberNode As System.Xml.XmlNode = xmlDoc.CreateElement("Members")

         create the child nodes
        Dim nameNode As System.Xml.XmlNode = xmlDoc.CreateElement("Name")
        nameNode.InnerText = txtName.Text
        Dim phoneNode As System.Xml.XmlNode = xmlDoc.CreateElement("Phone")
        phoneNode.InnerText = txtPhone.Text
        Dim addressNode As System.Xml.XmlNode = xmlDoc.CreateElement("Address")
        addressNode.InnerText = "This is my address"

         add the child nodes to the new node
        memberNode.AppendChild(nameNode)
        memberNode.AppendChild(phoneNode)
        memberNode.AppendChild(addressNode)

         add the new node to the file and save
        xmlDoc.DocumentElement.AppendChild(memberNode)
        xmlDoc.Save(xmlFile)
       
        Call DisplayRecords()


Creative I think :) Dont worry Im not satisfied just yet.... There has got to be a way to iterate through find the node block that needs to be updated and update it...

so I am a searching

vbMarkO
 
Its certainly possible to iterate through and find the person you wish to update, but there is one major issue with your current system that could cause a problem. You use the name to search, but the chances are in even a relatively small system there will be more than one person with the same name. Using an iterative system would be far easier if you had a unique ID for each person.
 
Sure I understand that and certainly agree with you.
Though in this case at the moment there are no duplicate names within this group though it is certianly possible there could be done the road.

Not sure how to make a UNique ID ... I mean I know we could use SS# but thats to personal for this group as there is no need for me to have access to that info...

Phone Number isnt specific enough.... So Im not sure what I would use


But I would love to see how I might iterate this as you mentioned even with this potential problem and see how I might iterate through and then update


vbMarkO
 
Probably the best thing to do is decide if you wish to continue doing all the XML by hand, this will give you more control but create more work, or use something like Serialisation.

If you are moving away from the raw xml you could create your own collection class (or use generics -what version of VB are you using btw?) and simply store / calculate a unique numeric ID for each record.
 
Well I figured out how to iterate through the nodes and update the nodes I need updated.

This is the code I used to get it done......


Code:
  xmlDoc.Load(xmlFile)
        For Each myNode As XmlNode In xmlDoc.DocumentElement.ChildNodes
            If myNode.FirstChild.InnerText = lstNames.SelectedItem Then
                 Updates xmlNode and its sibblings 
                Dim strName As String = txtName.Text
                Dim myArr() As String
                myArr = Split(strName, " ")
                strName = myArr(1) & ", " & myArr(0)
                myNode.FirstChild.InnerText = strName
                myNode.ChildNodes(1).InnerText = txtPhone.Text
                myNode.ChildNodes(2).InnerText = txtAddress.Text
                myNode.ChildNodes(3).InnerText = cboCity.Text
                myNode.ChildNodes(4).InnerText = txtState.Text
                myNode.ChildNodes(5).InnerText = txtZip.Text
                Dim strDates As New ArrayList
                Dim myString As String

                For Each itm As String In lstDates.Items
                    strDates.Add(itm)
                Next
                For i As Integer = 0 To strDates.Count - 1
                    myString &= strDates(i) & "\"
                Next
                myNode.ChildNodes(6).InnerText = myString 
                myNode.ChildNodes(7).InnerText = txtNote.Text


                xmlDoc.Save(xmlFile)

                Exit For
            End If
        Next



        xmlDoc = Nothing

It wasnt that hard after all.....

Want to say again thank all of you for this thread as it has really been an eye opener for me. I know I am only scratching the surface of xml but I am glad I started so Ill enjoy the learning as I go along...

vbMarkO
 
What a thread!

Hi,

Just wanted to say thanks and congratultations to the people who participated in this thread.

As I mentioned at the start of this thread, I know I dont get here as ofen as I should and I apologise for that but there are so many other things I need to do.

Anyway the point is I bought some book on XML programming at the same time as I started this thread. Which was like what vbMarkO had said, just an over view rather than real life examples. So when I found all the posts in this thread yesterday it was like an Aladins cave. There is more information in the thread than in most of the book.

Thanks again and I have got this bookmarked in my browser.

Cheers, Dave.
 
Back
Top