扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
Imports System.IO
创新互联公司专注于昌黎网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供昌黎营销型网站建设,昌黎网站制作、昌黎网页设计、昌黎网站官网定制、成都微信小程序服务,打造昌黎网络公司原创品牌,更为您提供昌黎网站排名全网营销落地服务。
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim f As New FileInfo("d:\doc.xml")
If f.Exists = False Then
f.Create()
f.Refresh()
End If
Dim s As String = ""
Dim sw As StreamWriter = f.CreateText()
s = "?xml version=""1.0"" encoding=""GB2312""?"
sw.WriteLine(s)
sw.WriteLine("doc")
sw.WriteLine(" assembly")
sw.WriteLine("userId34/userId")
sw.WriteLine("userName张三/userName")
sw.WriteLine("qxbz1/qxbz")
sw.WriteLine(" /assembly")
sw.WriteLine("/doc")
sw.Flush()
sw.Close()
Process.Start("d:\doc.xml")
End Sub
End Class
DataSet 和 DataTable都有现成的方法:WriteXml
DataTable tb = this.dataGridView1.DataSource as DataTable;
if(tb != null)
{
tb.WriteXml(@"C:\table.xml",true);
return;
}
DataView dv = this.dataGridView1.DataSource as DataView;
if(dv != null)
{
dv.Table.WriteXml(@"C:\table.xml",true);
return;
}
IList list = this.dataGridView1.DataSource as IList;
if(list != null)
{
//to do,如果是IList,就要你自己想办法导出了
//XmlDocument or XmlWriter都可以考虑
}
使用DataSet类来完成这项任务。
DataSet对象在传输时,是以XML流的形式而不是以COM形式传输的。DataSet对象可以读取XML数据文件或者数据流,从而将树型结构的XML数据转换成关系型的数据,如表(DataTable)、列(DataColumn)、行(DataRow)等。
在这个实例中,我们将XML文件命名为“xmlfile.xml”,具体内容如下:
?xml version=\"1.0\"standalone=”yes”?
Detail
Person
NameManish/Name
Age22/Age
/Person
/Detail \'用VB.NET读写XML文件
C1aSS WriteXML
Shared Sub main()
Dim obj DataSet As New System.Data.DataSet()
Dim strVirtualPath As String=”t.xml”
\'载入XML文件DataSet
objDataSet.ReadXml(”xmlfile.xml”)
\'通过控制器读取XML内容
Console.Write(objDataSet.GetXml)
\'从原XML文件中得到数据
ConSOle.Write(”Enter Name:”)
Dim fname,age As String
fname=ConS01e.ReadLine
\'写入你希望的代码
ConS01e.Write(”Enter Age:”)
age=Console.ReadLine
ConS0le.Write(fname&age)
Dim v(1)As String
v(0)=fname
V(1)=age
\'增加数据DataSet
objDataSet.Tables(0).Rows.Add(V)
\'更新XML文件
objDataSet.WriteXml(”xmlfile.xml”)
Console.Write(obj DataSet.GetXml)
End Sub
End C1ass
运行该程序有两种方式,一种是在Microso~VS.NET框架中编译完后运行;另一种则是在仿DOS的Command窗口中运行。在第一种方式中,必须加上System、System.Data、System.xML的引用。具体方法为:先创建一个空的项目,然后加入这个文件名字;在“解决方案资源管理器”中加入引用,此时需要选择相应的.dll文件;注意,xmlfile.xml文件应该放在工程的/bin目录下面。我们可以输出结果也可以用浏览器直接查看改变后的XML文件。在第二种方式中,选择桌面的“程序”一“Microso~Visual Studio.NET”一“VisualStudio.NET工具”一“Visual Studio.NET命令提示”命令,在DOS命令提示框敲入命令:[Page]
vbc/r:system.d11/r:system.data.d11/r:system.xml.d11 xml.vb其中,程序名为xml.vb,这里的3个.dll文件都是必须调用的库文件。应该在存放VB.NET文件的目录中运行上述命令,否则找不到相应的程序,当然设置PATH也可以做到。此外,Xml_vb文件应该和XML文件放在同一目录下面。每次运行此程序都会往文件中写进同样的内容,都是写入Person/,运行一次写一行。其实,我们只要稍微修改一下这个程序,就可以往XML中写入我们所需要的数据。
使用System.XML
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Xml
namespace HowTo.Samples.XML
public class WriteXmlFileSample
private const document as string = "newbooks.xml"
shared sub Main()
Dim myWriteXmlFileSample as WriteXmlFileSample
myWriteXmlFileSample = new WriteXmlFileSample()
myWriteXmlFileSample.Run(document)
end sub
public sub Run(args As String)
Dim myXmlTextReader as XmlTextReader = nothing
Dim myXmlTextWriter as XmlTextWriter = nothing
try
myXmlTextWriter = new XmlTextWriter (args, nothing)
myXmlTextWriter.Formatting = System.Xml.Formatting.Indented
myXmlTextWriter.WriteStartDocument(false)
myXmlTextWriter.WriteDocType("bookstore", nothing, "books.dtd", nothing)
myXmlTextWriter.WriteComment("此文件表示书店库存数据库的另一个片断")
myXmlTextWriter.WriteStartElement("bookstore")
myXmlTextWriter.WriteStartElement("book", nothing)
myXmlTextWriter.WriteAttributeString("genre","autobiography")
myXmlTextWriter.WriteAttributeString("publicationdate","1979")
myXmlTextWriter.WriteAttributeString("ISBN","0-7356-0562-9")
myXmlTextWriter.WriteElementString("title", nothing, "The Autobiography of Mark Twain")
myXmlTextWriter.WriteStartElement("Author", nothing)
myXmlTextWriter.WriteElementString("first-name", "Mark")
myXmlTextWriter.WriteElementString("last-name", "Twain")
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteElementString("price", "7.99")
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteEndElement()
'向文件写 XML 并关闭编写器
myXmlTextWriter.Flush()
myXmlTextWriter.Close()
' 读取返回的文件并进行分析以确保正确生成 XML
myXmlTextReader = new XmlTextReader (args)
FormatXml (myXmlTextReader, args)
catch e as Exception
Console.WriteLine ("异常:{0}", e.ToString())
finally
Console.WriteLine()
Console.WriteLine("对文件 {0} 的处理已完成。", args)
If Not myXmlTextReader Is Nothing
myXmlTextReader.Close()
end if
'关闭编写器
If Not myXmlTextWriter Is Nothing
myXmlTextWriter.Close()
end if
End try
End Sub
private shared Sub FormatXml (reader as XmlTextReader, filename as String)
Dim piCount, docCount, commentCount, elementCount as Integer
Dim attributeCount, textCount, whitespaceCount as Integer
While reader.Read()
Select (reader.NodeType)
case XmlNodeType.ProcessingInstruction:
Format (reader, "ProcessingInstruction")
piCount += 1
case XmlNodeType.DocumentType:
Format (reader, "DocumentType")
docCount += 1
case XmlNodeType.Comment:
Format (reader, "Comment")
commentCount += 1
case XmlNodeType.Element:
Format (reader, "Element")
elementCount += 1
While reader.MoveToNextAttribute()
Format (reader, "Attribute")
end While
if (reader.HasAttributes)
attributeCount += reader.AttributeCount
end if
case XmlNodeType.Text:
Format (reader, "Text")
textCount += 1
case XmlNodeType.Whitespace:
whitespaceCount += 1
End Select
End While
' 显示该文件的统计信息
Console.WriteLine ()
Console.WriteLine("{0} 文件的统计信息", filename)
Console.WriteLine ()
Console.WriteLine("处理指令:" piCount)
Console.WriteLine("文档类型:" docCount)
Console.WriteLine("注释:" commentCount)
Console.WriteLine("元素:" elementCount)
Console.WriteLine("属性:" attributeCount)
Console.WriteLine("文本:" textCount)
Console.WriteLine("空白:" whitespaceCount)
End Sub
private shared Sub Format(byref reader as XmlTextReader , NodeType as String)
' 格式化输出
Console.Write(reader.Depth " ")
Console.Write(reader.AttributeCount " ")
Dim i as Integer
for i = 0 to reader.Depth - 1
Console.Write(Strings.chr(9))
Next
Console.Write(reader.Prefix NodeType "" reader.Name "" reader.Value)
Console.WriteLine()
End Sub
End Class
End Namespace
参考:
Dim s As String = "ipconfig" vbNewLine " address10.3.11.162/address" vbNewLine "/ipconfig"
System.IO.File.WriteAllText("C:\我的文件夹\我的文件.xml", s)
你也可以把内容放进textbox中,然后把textbox的内容写进去:
System.IO.File.WriteAllText("C:\我的文件夹\我的文件.xml", textbox1.text)
在紧挨着类或成员的上一行打三个单引号,自动生成XML注释。
你可以使用para来换行。
使用see cref来标明参考项。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流