Imports System.IO
創(chuàng)新互聯(lián)公司專(zhuān)注于昌黎網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供昌黎營(yíng)銷(xiāo)型網(wǎng)站建設(shè),昌黎網(wǎng)站制作、昌黎網(wǎng)頁(yè)設(shè)計(jì)、昌黎網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造昌黎網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供昌黎網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
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都有現(xiàn)成的方法: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,就要你自己想辦法導(dǎo)出了
//XmlDocument or XmlWriter都可以考慮
}
使用DataSet類(lèi)來(lái)完成這項(xiàng)任務(wù)。
DataSet對(duì)象在傳輸時(shí),是以XML流的形式而不是以COM形式傳輸?shù)?。DataSet對(duì)象可以讀取XML數(shù)據(jù)文件或者數(shù)據(jù)流,從而將樹(shù)型結(jié)構(gòu)的XML數(shù)據(jù)轉(zhuǎn)換成關(guān)系型的數(shù)據(jù),如表(DataTable)、列(DataColumn)、行(DataRow)等。
在這個(gè)實(shí)例中,我們將XML文件命名為“xmlfile.xml”,具體內(nèi)容如下:
?xml version=\"1.0\"standalone=”yes”?
Detail
Person
NameManish/Name
Age22/Age
/Person
/Detail \'用VB.NET讀寫(xiě)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”)
\'通過(guò)控制器讀取XML內(nèi)容
Console.Write(objDataSet.GetXml)
\'從原XML文件中得到數(shù)據(jù)
ConSOle.Write(”Enter Name:”)
Dim fname,age As String
fname=ConS01e.ReadLine
\'寫(xiě)入你希望的代碼
ConS01e.Write(”Enter Age:”)
age=Console.ReadLine
ConS0le.Write(fname&age)
Dim v(1)As String
v(0)=fname
V(1)=age
\'增加數(shù)據(jù)DataSet
objDataSet.Tables(0).Rows.Add(V)
\'更新XML文件
objDataSet.WriteXml(”xmlfile.xml”)
Console.Write(obj DataSet.GetXml)
End Sub
End C1ass
運(yùn)行該程序有兩種方式,一種是在Microso~VS.NET框架中編譯完后運(yùn)行;另一種則是在仿DOS的Command窗口中運(yùn)行。在第一種方式中,必須加上System、System.Data、System.xML的引用。具體方法為:先創(chuàng)建一個(gè)空的項(xiàng)目,然后加入這個(gè)文件名字;在“解決方案資源管理器”中加入引用,此時(shí)需要選擇相應(yīng)的.dll文件;注意,xmlfile.xml文件應(yīng)該放在工程的/bin目錄下面。我們可以輸出結(jié)果也可以用瀏覽器直接查看改變后的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個(gè).dll文件都是必須調(diào)用的庫(kù)文件。應(yīng)該在存放VB.NET文件的目錄中運(yùn)行上述命令,否則找不到相應(yīng)的程序,當(dāng)然設(shè)置PATH也可以做到。此外,Xml_vb文件應(yīng)該和XML文件放在同一目錄下面。每次運(yùn)行此程序都會(huì)往文件中寫(xiě)進(jìn)同樣的內(nèi)容,都是寫(xiě)入Person/,運(yùn)行一次寫(xiě)一行。其實(shí),我們只要稍微修改一下這個(gè)程序,就可以往XML中寫(xiě)入我們所需要的數(shù)據(jù)。
使用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("此文件表示書(shū)店庫(kù)存數(shù)據(jù)庫(kù)的另一個(gè)片斷")
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()
'向文件寫(xiě) XML 并關(guān)閉編寫(xiě)器
myXmlTextWriter.Flush()
myXmlTextWriter.Close()
' 讀取返回的文件并進(jìn)行分析以確保正確生成 XML
myXmlTextReader = new XmlTextReader (args)
FormatXml (myXmlTextReader, args)
catch e as Exception
Console.WriteLine ("異常:{0}", e.ToString())
finally
Console.WriteLine()
Console.WriteLine("對(duì)文件 {0} 的處理已完成。", args)
If Not myXmlTextReader Is Nothing
myXmlTextReader.Close()
end if
'關(guān)閉編寫(xiě)器
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
' 顯示該文件的統(tǒng)計(jì)信息
Console.WriteLine ()
Console.WriteLine("{0} 文件的統(tǒng)計(jì)信息", filename)
Console.WriteLine ()
Console.WriteLine("處理指令:" piCount)
Console.WriteLine("文檔類(lèi)型:" docCount)
Console.WriteLine("注釋?zhuān)? 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)
你也可以把內(nèi)容放進(jìn)textbox中,然后把textbox的內(nèi)容寫(xiě)進(jìn)去:
System.IO.File.WriteAllText("C:\我的文件夾\我的文件.xml", textbox1.text)
在緊挨著類(lèi)或成員的上一行打三個(gè)單引號(hào),自動(dòng)生成XML注釋。
你可以使用para來(lái)?yè)Q行。
使用see cref來(lái)標(biāo)明參考項(xiàng)。