SimonPrice33
asked on
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>
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
Are you saying you don't want any leading whitespace before the nodes?
ASKER
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...
<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...
ASKER
I have only entered spaces between the node and the data to make it easier to see how i want it
ASKER
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?
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")
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
XmlDocument is rather simple to use. There is an example of adding nodes here: http://msdn.microsoft.com/en-us/library/ms162365.aspx
ASKER
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...
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...
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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?
ASKER
it would be occuring in the same code... with a new transaction every few minutes.
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.
ASKER
not being totally up on .Net 2.0 way of doing this can you show me how to do this please.
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
Usage
Dim xdoc As Xml.XmlDocument = GenerateXmlDocument()
AppendTransaction("abc", "def", "ghi", "jkl", "mno", xdoc)
ASKER
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?
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?
ASKER
thanks