• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

write xml in vb.net 2.0

I am currently working in .Net 2.0 and need to write an XML file

so far I have managed to find code that will push out to XML but will only write to the same line.
What I need os for new data to be written down the page like so

<Samples>
<sample>
<SationID> C1 </StationID>
<Date> 01/01/2011 </date>
<Time> 00:00 </time
<stub> 11111111A </stub>
<weight> 126 </Weight>
<tops> 4 </tops>
<Sample>
<sample>
<SationID> C1 </StationID>
<Date> 01/01/2011 </date>
<Time> 00:00 </time
<stub> 11111111A </stub>
<weight> 126 </Weight>
<tops> 4 </tops>
<Sample>
<sample>
<SationID> C1 </StationID>
<Date> 01/01/2011 </date>
<Time> 00:00 </time
<stub> 11111111A </stub>
<weight> 126 </Weight>
<tops> 4 </tops>
<Sample>
</Samples>

which is currently showing as

<Samples>
  <Sample Stub="1111111A" Date="21/10/2011" Time="14:12:36" Weight="126" Tops="4" />
  </Samples>


Imports System.Xml
Imports System.IO


Public Class Form1
    Dim path As String = "\\contfs02\public\simonprice\tarehouse\tarehouserecordings.xml"
    Dim sw As StreamWriter = New StreamWriter(path)


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        dateLbl.Text = DateTime.Today
        TimeLbl.Text = Date.Now.Hour & ":" & Date.Now.Minute & ":" & Date.Now.Second

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


       
        Dim XMLobj As Xml.XmlTextWriter
        Dim enc As New System.[Text].UnicodeEncoding()
        XMLobj = New Xml.XmlTextWriter("C:\test.xml", enc)
        XMLobj.Formatting = Xml.Formatting.Indented
        XMLobj.Indentation = 3
        XMLobj.WriteStartDocument()
        XMLobj.WriteStartElement("Samples")
        XMLobj.WriteStartElement("Sample")
        XMLobj.WriteAttributeString("Stub", stubTxt.Text)
        XMLobj.WriteAttributeString("Date", dateLbl.Text)
        XMLobj.WriteAttributeString("Time", TimeLbl.Text)
        XMLobj.WriteAttributeString("Weight", cleanTxt.Text)
        XMLobj.WriteAttributeString("Tops", topsTxt.Text)
        XMLobj.WriteEndElement()
        XMLobj.WriteEndElement()
        XMLobj.Close()
        MsgBox("Done", MsgBoxStyle.Exclamation, "XML")


    End Sub
End Class

Open in new window

0
SimonPrice33
Asked:
SimonPrice33
  • 10
  • 6
1 Solution
 
käµfm³d 👽Commented:
Are you saying you don't want any leading whitespace before the nodes?
0
 
SimonPrice33Author Commented:
the way I want it to write out is like this


<Samples>
   <sample>
     <SationID> C1 </StationID>
     <Date> 01/01/2011 </date>
     <Time> 00:00 </time
     <stub> 11111111A </stub>
     <weight> 126 </Weight>
     <tops> 4 </tops>
  <Sample>
</Samples>

Ive never written XML in .Net 2.0 before only 3.5 and its different to what I was expecting...

0
 
SimonPrice33Author Commented:
I have only entered spaces between the node and the data to make it easier to see how i want it

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
SimonPrice33Author Commented:
right....

have got it to display how i want... now all I need is to get it to append new records to it...

any suggestions?

'Dim XMLobj As Xml.XmlTextWriter
        Dim writer As Xml.XmlWriter
        Dim enc As New System.[Text].UnicodeEncoding()
        writer = New Xml.XmlTextWriter("C:\test.xml", enc)

        'writer.Formatting = Xml.Formatting.Indented
        'writer.Indentation = 3
        writer.WriteStartDocument()
        writer.WriteStartElement("Samples")
        writer.WriteStartElement("Sample")
        writer.WriteAttributeString("Stub", stubTxt.Text)
        writer.WriteEndElement()
        writer.WriteStartElement("Date", dateLbl.Text)
        writer.WriteEndElement()
        writer.WriteStartElement("Time", TimeLbl.Text)
        writer.WriteEndElement()
        writer.WriteStartElement("Weight", cleanTxt.Text)
        writer.WriteEndElement()
        writer.WriteStartElement("Tops", topsTxt.Text)
        writer.WriteEndElement()
        writer.WriteEndElement()
        writer.Close()
        MsgBox("Done", MsgBoxStyle.Exclamation, "XML")

Open in new window

0
 
käµfm³d 👽Commented:
My suggestion: work with an XmlDocument vs. an XmlTextWriter. The reason is you get methods that allow you to add nodes more easily. With your current approach, you have to go back and insert code into the proper position whenever you want to add a new node. This would be overwhelming as the number of changes increases.

XmlDocument is rather simple to use. There is an example of adding nodes here: http://msdn.microsoft.com/en-us/library/ms162365.aspx
0
 
SimonPrice33Author Commented:
I will take a look now...the nodes should never change once Ive got it working.....

this is for an application that has been in place for a number of years... as you can see, records date time, station id weight and topweight...
0
 
SimonPrice33Author Commented:
that doesnt look as straightforward to me as the way i currently have it....  can you give me some guidance on how I could get it to work in a similar fashion to how I have it and how i want it please
0
 
käµfm³d 👽Commented:
the nodes should never change once Ive got it working.....
It's a matter of preference. Either approach should work; each just has its own subtleties.

can you give me some guidance on how I could get it to work in a similar fashion to how I have it and how i want it please
Here is an XmlDocument example.

Dim xdoc As New Xml.XmlDocument()

Dim samples As Xml.XmlNode = xdoc.AppendChild(xdoc.CreateElement("Samples"))
Dim sample As Xml.XmlNode = samples.AppendChild(xdoc.CreateElement("Sample"))

sample.Attributes.Append(xdoc.CreateAttribute("Stub")).InnerText = stubTxt.Text
samples.AppendChild(xdoc.CreateElement("Date")).InnerText = dateLbl.Text
samples.AppendChild(xdoc.CreateElement("Time")).InnerText = TimeLbl.Text
samples.AppendChild(xdoc.CreateElement("Weight")).InnerText = cleanTxt.Text
samples.AppendChild(xdoc.CreateElement("Tops")).InnerText = topsTxt.Text

xdoc.Save("C:\test.xml")

Open in new window

0
 
SimonPrice33Author Commented:
Hi

Thats great thank you, what i now need to have is for the next records to appear under the previous records.

Can you help me with that please.

Thank you
0
 
käµfm³d 👽Commented:
Assuming you were using my snippet above, would this addition be occurring in the same code above, or would you be loading this document at some later date and inserting the new data?
0
 
SimonPrice33Author Commented:
it would be occuring in the same code...   with a new transaction every few minutes.
0
 
käµfm³d 👽Commented:
Once you have the initial document created (i.e. once the code above is run at least once), you can maintain the reference to your samples node. This will allow you to again call the AppendChild to insert another node. In other words, when a new transaction occurs, repeat lines 4 - 12.
0
 
SimonPrice33Author Commented:
not being totally up on .Net 2.0 way of doing this can you show me how to do this please.
0
 
käµfm³d 👽Commented:
Function GenerateXmlDocument() As Xml.XmlDocument
    Dim xdoc As New Xml.XmlDocument()

    Dim samples As Xml.XmlNode = xdoc.AppendChild(xdoc.CreateElement("Samples"))
    Dim sample As Xml.XmlNode = samples.AppendChild(xdoc.CreateElement("Sample"))

    sample.Attributes.Append(xdoc.CreateAttribute("Stub")).InnerText = stubTxt.Text
    samples.AppendChild(xdoc.CreateElement("Date")).InnerText = dateLbl.Text
    samples.AppendChild(xdoc.CreateElement("Time")).InnerText = TimeLbl.Text
    samples.AppendChild(xdoc.CreateElement("Weight")).InnerText = cleanTxt.Text
    samples.AppendChild(xdoc.CreateElement("Tops")).InnerText = topsTxt.Text

    xdoc.Save("C:\test.xml")

    Return xdoc
End Function

Sub AppendTransaction(ByVal stub As String, ByVal transDate As String, ByVal transTime As String, ByVal weight As String, ByVal tops As String, ByRef xdoc As Xml.XmlDocument)
    Dim root As XmlNode = xdoc.SelectSingleNode("/Samples")
    
    If root IsNot Nothing Then
        Dim sample As Xml.XmlNode = root.AppendChild(xdoc.CreateElement("Sample"))

        sample.Attributes.Append(xdoc.CreateAttribute("Stub")).InnerText = stub
        root.AppendChild(xdoc.CreateElement("Date")).InnerText = transDate
        root.AppendChild(xdoc.CreateElement("Time")).InnerText = transTime
        root.AppendChild(xdoc.CreateElement("Weight")).InnerText = weight
        root.AppendChild(xdoc.CreateElement("Tops")).InnerText = tops
    End If
    
    xdoc.Save("C:\test.xml")
End Sub

Open in new window


Usage
Dim xdoc As Xml.XmlDocument = GenerateXmlDocument()

AppendTransaction("abc", "def", "ghi", "jkl", "mno", xdoc)

Open in new window

0
 
SimonPrice33Author Commented:
Hi,

your example appeneded a second record to an existing record but no futher records thereafter...  and then any new records change the two records that are now in place.....

Can you offer any advice?
0
 
SimonPrice33Author Commented:
thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now