Click here to Skip to main content
15,887,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>

The code I am using to generate this is

VB
'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.WriteAttributeString("Date", dateLbl.Text)
writer.WriteAttributeString("Time", TimeLbl.Text)
writer.WriteAttributeString("Weight", cleanTxt.Text)
writer.WriteAttributeString("Tops", topsTxt.Text)
writer.WriteEndElement()
writer.WriteEndElement()
writer.Close()
MsgBox("Done", MsgBoxStyle.Exclamation, "XML")



Any help will be very much appreciated.
Posted

Hi,

You just have to use "writer.WriteName" instead of "writer.WriteAttributeString" function.

VB
'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")

'Stub
writer.WriteStartElement("Stub")
writer.WriteName(stubTxt.Text)
writer.WriteEndElement()

'Date
writer.WriteStartElement("Date")
writer.WriteName(dateLbl.Text)
writer.WriteEndElement()

'Time
writer.WriteStartElement("Time")
writer.WriteName(TimeLbl.Text)
writer.WriteEndElement()

'Weight
writer.WriteStartElement("Weight")
writer.WriteName(cleanTxt.Text)
writer.WriteEndElement()

'Tops
writer.WriteStartElement("Tops")
writer.WriteName(topsTxt.Text)
writer.WriteEndElement()

writer.WriteEndElement()
writer.WriteEndElement()
writer.Close()
MsgBox("Done", MsgBoxStyle.Exclamation, "XML")


Hope this solution will help to solve your problem.

Regards,
Ambicaprasad Maurya
 
Share this answer
 
Hi,

I have just tried this nad get an invalid character error in stubTxt.text.

I had moments before got it to present as I want usuing

'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")

But what I need now is for each new record to be appeneded to it rather than overwrite it.

Thanks
 
Share this answer
 
Try using the XmlWriterSettings:
VB
Dim writer As XmlWriter
Dim settings As New XmlWriterSettings()
settings.Indent = True
settings.NewLineOnAttributes = True
writer = XmlWriter.Create("D:\Temp\test.xml", settings)
writer.WriteStartDocument()
writer.WriteStartElement("Samples")
writer.WriteStartElement("Sample")
writer.WriteAttributeString("Stub", "MyStub")
writer.WriteAttributeString("Date", "MyDate")
writer.WriteAttributeString("Time", "MyTime")
writer.WriteAttributeString("Weight", "MyWewight")
writer.WriteAttributeString("Tops", "MyTops")
writer.WriteEndElement()
writer.WriteEndElement()
writer.Close()
 
Share this answer
 
That didnt work as I want....

it come out as

XML
<Samples>
  <Sample Stub="MyStub" Date="MyDate" Time="MyTime" Weight="MyWewight" Tops="MyTops" />
  </Samples>


When Im looking for it to look like

XML
<Samples>
- <Sample>
  <Stub xmlns="12" />
  <Date xmlns="21/10/2011" />
  <Time xmlns="15:25:59" />
  <Weight xmlns="23" />
  <Tops xmlns="34" />
  </Sample>
  </Samples>



just without the xmlns

But as I will have records being written to it every few seconds I need the new records to be appeneded to it too

Any suggestions?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900