问题描述
我目前正在.Net 2.0中工作,需要编写XML文件
到目前为止,我设法找到了可以推送到XML但只写到同一行的代码.
我需要os以便将新数据这样写在页面上
<样品>
< sample>
< SationID> C1</StationID>
<日期> 2011年1月1日</日期>
<时间> 00:00</time
< stub> 11111111A</stub>
< weight> 126</Weight>
< tops> 4</tops>
<样品>
< sample>
< SationID> C1</StationID>
<日期> 2011年1月1日</日期>
<时间> 00:00</time
< stub> 11111111A</stub>
< weight> 126</Weight>
< tops> 4</tops>
<样品>
< sample>
< SationID> C1</StationID>
<日期> 2011年1月1日</日期>
<时间> 00:00</time
< stub> 11111111A</stub>
< weight> 126</Weight>
< tops> 4</tops>
<样品>
</样品>
当前显示为
<样品>
< Sample Stub ="1111111A" Date ="21/10/2011" Time ="14:12:36" Weight ="126" Tops ="4"/>
</样品>
我用来生成此代码的代码是
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
'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.
推荐答案
'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")
希望此解决方案将帮助您解决问题.
问候,
Ambicaprasad Maurya
Hope this solution will help to solve your problem.
Regards,
Ambicaprasad Maurya
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()
这篇关于在vb.net 2.0中写入xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!