最好使用LINQ(全名為:.NET Language Query framework)的功能,讀寫都很方便的。自己去到網(wǎng)上找一找。
成都創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括瓊海網(wǎng)站建設(shè)、瓊海網(wǎng)站制作、瓊海網(wǎng)頁(yè)制作以及瓊海網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,瓊海網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到瓊海省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
使用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("此文件表示書店庫(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()
'向文件寫 XML 并關(guān)閉編寫器
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)閉編寫器
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("文檔類型:" 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
參考:
使用DataSet類來(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讀寫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
\'寫入你希望的代碼
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ì)往文件中寫進(jìn)同樣的內(nèi)容,都是寫入Person/,運(yùn)行一次寫一行。其實(shí),我們只要稍微修改一下這個(gè)程序,就可以往XML中寫入我們所需要的數(shù)據(jù)。
Dim?xmlDoc?As?New?System.Xml.XmlDocument
xmlDoc.Load("c:\xml.xml")?'載入xml文件
Dim?Items?As?Xml.XmlNodeList?=?xmlDoc.DocumentElement.SelectNodes("http://record/item")?'參數(shù)為xpath查詢串,前面斜杠,//:表示任何結(jié)點(diǎn),/:表示根結(jié)點(diǎn)
For?Each?s?As?Xml.XmlNode?In?Items
Console.WriteLine(s.Attributes.GetNamedItem("id").Value??vbTab??s.InnerText)
Next
先讀取節(jié)點(diǎn)Match,然后讀取節(jié)點(diǎn)內(nèi)的各個(gè)屬性值。 再進(jìn)行節(jié)點(diǎn)中的子節(jié)點(diǎn)SetSetName的讀取,讀取方式也是直接讀取各個(gè)屬性值。
將下面的代碼,放在Button1的Click事件里
textbox1.Text = System.IO.File.ReadAllText("c:\1.xml", System.Text.Encoding.Default)