StealthRT
StealthRT

Reputation: 10542

XML appending to current xml file

I am having a hard time trying to append to an XML file I create. This is currently the code I have to create the XML file:

 Dim myXmlTextWriter As New XmlTextWriter("d:\doc.xml", Nothing)

 With myXmlTextWriter
        .Formatting = Formatting.Indented
        .Indentation = 3
        .IndentChar = " "

        .WriteStartDocument()
        .WriteComment("Data for 3030")
        .WriteStartElement("3030")
        .WriteElementString("jod", "364887")
        .WriteElementString("aag_SN", "782 YvV0007")
        .WriteElementString("te", "9.03")

        .WriteEndElement()
        .Close()
 End With

Every time I run this code, it just overwrites the existing data. How can I append to the existing data and still have the same structure? I can't loop for all my data because the user will fill something out each time and can't move on until it's saved first.

Here is an example of what I am looking for:

 <?xml version="1.0"?>
 <!--Data for 3030-->
 <3030>
    <jod>364887</jod>
    <aag_SN>782 YvV0007</aag_SN>
    <te>9.03</te>
    <jod>364337</jod>
    <aag_SN>782 Y089702</aag_SN>
    <te>5.00</te>
    <jod>32687</jod>
    <aag_SN>782 YFd3407</aag_SN>
    <te>2.43</te>
    <jod>39007</jod>
    <aag_SN>782 Yv75407</aag_SN>
    <te>3.03</te>
 </3030>

So I need to read the values in each time I start a new insert, but I am unable to find code to do what I currently need.

Any help would be great!

David

Upvotes: 1

Views: 9802

Answers (2)

Mark Hall
Mark Hall

Reputation: 54532

Looking at this Daniweb question one of the answers appears to work. What he suggests is to create the FileStream yourself with the append option, then use this stream when you create your XmlTextWriter. You will have to check if the file exists in order to write the XmlHeader only one time.

Dim writeStart As Boolean
If Not IO.File.Exists("C:\temp\doc.xml") Then writeStart = True
Dim xmlFile As IO.FileStream = New IO.FileStream("c:\temp\doc.xml", IO.FileMode.Append)
Dim myXmlTextWriter As New XmlTextWriter(xmlFile, System.Text.Encoding.Default)

With myXmlTextWriter
    .Formatting = Formatting.Indented
    .Indentation = 3
    .IndentChar = CChar(" ")
    If writeStart Then .WriteStartDocument()
    .WriteComment("Data for 3030")
    .WriteStartElement("3030")
    .WriteElementString("jod", "364887")
    .WriteElementString("aag_SN", "782 YvV0007")
    .WriteElementString("te", "9.03")
    .WriteFullEndElement()
    .Close()
End With

I made some changes because of the multiple root object, the xmlDocument reader is also complaining about your Tags starting with a number i.e. "3030" so I prepended an alpha character. I am using the XmlTextWriter for creation of the file only:

Dim writeStart As Boolean
If Not IO.File.Exists("C:\temp\doc.xml") Then writeStart = True

Dim xmlFile As IO.FileStream = New IO.FileStream("c:\temp\doc.xml", IO.FileMode.Append)
Dim myXmlTextWriter As New XmlTextWriter(xmlFile, System.Text.Encoding.Default)
If writeStart Then
    With myXmlTextWriter
        .Formatting = Formatting.Indented
        .Indentation = 3
        .IndentChar = CChar(" ")
        .WriteStartDocument()
        .WriteStartElement("root")
        .WriteEndElement()
    End With
End If
myXmlTextWriter.Close()

AddXmlData("c:\temp\doc.xml", "a3030", "364887", "782 YvV0007", "9.03")

Add I am then using this Sub to add your data:

Private Sub AddXmlData(xmlfile As String, index As String, jod As String, aag_SN As String, te As String)
    Dim myXmlDocument As New XmlDocument
    Dim myNodes, myChildren As XmlNodeList
    Dim node(3) As XmlNode

    myXmlDocument.Load(xmlfile)
    myNodes = myXmlDocument.GetElementsByTagName("root")

    For Each n As XmlNode In myNodes
        If n.Name = "root" Then
            myChildren = n.ChildNodes
            For Each n1 As XmlNode In myChildren
                If n1.Name = index Then
                    node(1) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "jod", "")
                    node(2) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "aag_SN", "")
                    node(3) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "te", "")
                    node(1).InnerText = jod
                    node(2).InnerText = aag_SN
                    node(3).InnerText = te
                    n1.AppendChild(node(1))
                    n1.AppendChild(node(2))
                    n1.AppendChild(node(3))
                    myXmlDocument.Save(xmlfile)
                    Exit Sub
                End If
            Next
            node(0) = myXmlDocument.CreateNode(XmlNodeType.Element, index, "")
            node(1) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "jod", "")
            node(2) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "aag_SN", "")
            node(3) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "te", "")
            node(1).InnerText = jod
            node(2).InnerText = aag_SN
            node(3).InnerText = te
            node(0).AppendChild(node(1))
            node(0).AppendChild(node(2))
            node(0).AppendChild(node(3))
            n.AppendChild(node(0))
            myXmlDocument.Save(xmlfile)
        End If
    Next
End Sub

Which is creating an xmlDocument looking like this:

<?xml version="1.0" encoding="Windows-1252"?>
<root>
  <a3030>
    <jod>364887</jod>
    <aag_SN>782 YvV0007</aag_SN>
    <te>9.03</te>
    <jod>364887</jod>
    <aag_SN>782 YvV0007</aag_SN>
    <te>9.03</te>
    <jod>364887</jod>
    <aag_SN>782 YvV0007</aag_SN>
    <te>9.03</te>
    <jod>364887</jod>
    <aag_SN>782 YvV0007</aag_SN>
    <te>9.03</te>
  </a3030>
</root>

Upvotes: 3

Steve Wellens
Steve Wellens

Reputation: 20620

You have to rewrite the entire file. It is after all, just a text file.

If the data is small, it might be easier to use XmlDocument.

Upvotes: 0

Related Questions