DataSet 和 DataTable都有現(xiàn)成的方法:WriteXml
我們注重客戶提出的每個要求,我們充分考慮每一個細節(jié),我們積極的做好網(wǎng)站建設、成都網(wǎng)站制作服務,我們努力開拓更好的視野,通過不懈的努力,成都創(chuàng)新互聯(lián)贏得了業(yè)內(nèi)的良好聲譽,這一切,也不斷的激勵著我們更好的服務客戶。 主要業(yè)務:網(wǎng)站建設,網(wǎng)站制作,網(wǎng)站設計,微信小程序開發(fā),網(wǎng)站開發(fā),技術開發(fā)實力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫的技術開發(fā)工程師。
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都可以考慮
}
有兩種辦法。
方法一、創(chuàng)建一個 MSXML2.DOMDocument 對象,把這個文件Load進來,按照教科書上的步驟處理。這個對象名并非唯一,依據(jù)你電腦上的MSXML版本而定。你在引用中查一下。
方法二、將這個文件當做一個普通的文本文件來處理。將它完整的讀入到一個字符串中,依據(jù)節(jié)點名用Split()函數(shù)將它分段,提取所需的內(nèi)容。
兩種方法都不復雜,嘗試一下吧。
使用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ù)據(jù)庫的另一個片斷")
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
' 顯示該文件的統(tǒng)計信息
Console.WriteLine ()
Console.WriteLine("{0} 文件的統(tǒng)計信息", 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?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查詢串,前面斜杠,//:表示任何結點,/:表示根結點
For?Each?s?As?Xml.XmlNode?In?Items
Console.WriteLine(s.Attributes.GetNamedItem("id").Value??vbTab??s.InnerText)
Next
分享題目:vb.net處理xml的簡單介紹
標題URL:http://muchs.cn/article22/doedejc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、外貿(mào)網(wǎng)站建設、網(wǎng)頁設計公司、App設計、網(wǎng)站排名、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)