在vb.net 2.0中写入xml [英] write xml in vb.net 2.0

查看:75
本文介绍了在vb.net 2.0中写入xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在.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.

推荐答案



您只需要使用"writer.WriteName"而不是"writer.WriteAttributeString"函数.

Hi,

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

'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




我刚刚尝试过此操作,但在stubTxt.text中得到了无效的字符错误.

我想使用它之前要先介绍一下

''将XMLobj格式化为Xml.XmlTextWriter
作为Xml.XmlWriter的昏暗作家
昏暗的环境作为新系统.[文本] .UnicodeEncoding()
writer =新的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")

但是我现在需要的是将每个新记录添加到该记录而不是覆盖它.

谢谢
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


尝试使用XmlWriterSettings:
Try using the XmlWriterSettings:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆