vb.netxml修改的簡(jiǎn)單介紹

利用vb對(duì)XML如何進(jìn)行修改?

利用DataTable,讀取XML,DataTable。ReadXML(Filepath),然后就可以像修改表格那樣改了。

創(chuàng)新互聯(lián)建站成立于2013年,先為赫章等服務(wù)建站,赫章等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為赫章企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

VB.NET 或者 C#實(shí)現(xiàn)XML樹增刪改節(jié)點(diǎn) 代碼

//創(chuàng)建XMLdocument

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

// 從XML文件中加載XML

doc.Load(XmlPath);

//為doc的根節(jié)點(diǎn)創(chuàng)建子節(jié)點(diǎn)nodeA(沒有添加到根節(jié)點(diǎn)上?。?/p>

System.Xml.XmlNode nodeA = doc.CreateNode(System.Xml.XmlNodeType.Element, "SAMPLE_ADD", "SAMPLEURI_ADD");

//為子節(jié)點(diǎn)nodeA設(shè)置屬性

nodeA.Value = "SAMPLE VALUE ADD";

//將nodeA添加為doc的子節(jié)點(diǎn)

doc.AppendChild(nodeA);

//為nodeA節(jié)點(diǎn)創(chuàng)建子節(jié)點(diǎn)nodeAA(沒有添加到nodeA節(jié)點(diǎn)上!)

System.Xml.XmlNode nodeAA = nodeA.CreateNode(System.Xml.XmlNodeType.Element, "SAMPLE_ADD2", "SAMPLEURI_ADD2");

//為子節(jié)點(diǎn)nodeAA設(shè)置屬性

nodeAA.Value = "SAMPLE VALUE ADD2";

//將nodeAA添加為nodeA的子節(jié)點(diǎn)

nodeA.AppendChild(nodeAA);

//遍歷nodeA下面的所有子節(jié)點(diǎn)

foreach (System.Xml.XmlNode node in nodeA.ChildNodes)

{

//處理這些節(jié)點(diǎn)

}

//刪除節(jié)點(diǎn)的做法是遍歷該節(jié)點(diǎn)然后吧符合條件的刪除掉

foreach (System.Xml.XmlNode node in doc.ChildNodes)

{

// 將節(jié)點(diǎn)從父上刪除

doc.RemoveChild(node);

}

vb.net操作xml數(shù)據(jù)庫(kù)(急)

使用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

參考:

VB6.0如何讀取并修改XML文件?

你有兩個(gè)選擇:

1、將xml文件當(dāng)做文本文件來(lái)處理。替換指定字符即可。

2、使用 MSXML 組件來(lái)處理。度娘搜一下,一大堆文檔可參考。

打字不易,如滿意,望采納。

VB.NET修改替換xml文件中的值

Dim path As String = PDA_PATH "ife.XML" ’PDA_PATH 為路徑

Dim ds As New DataSet

ds.ReadXml(path)

Dim dt As DataTable = ds.Tables.Item(0)

Dim blnY As Boolean = True

For Each row As DataRow In dt.Rows

If row.Item("Translation").ToString.ToUpper ="確認(rèn)"Then

row.Item("Translation") =“替換”

blnY = False

Exit For

End If

Next

If blnY Then

MsgBox("輸入的XXX不存在,請(qǐng)重新輸入! ")

Return

End If

ds.WriteXml(path)

MessageBox.Show("修改數(shù)據(jù)并保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)

或者

Dim xmlDoc As New XmlDocument()

'Dim nodeList As New XmlNodeList

xmlDoc.Load("life..xml") '加載

Dim nodeList As XmlNodeList = xmlDoc.SelectSingleNod.("UITranslations").ChildNodes '獲取bookstore節(jié)點(diǎn)的所有子節(jié)點(diǎn)

Dim xn As XmlNode

For Each xn In nodeList '遍歷所有子節(jié)點(diǎn)

Dim xe As XmlElement = CType(xn, XmlElement) '將子節(jié)點(diǎn)類型轉(zhuǎn)換為XmlElement類型

Dim nls As XmlNodeList = xe.ChildNodes '繼續(xù)獲取xe子節(jié)點(diǎn)的所有子節(jié)點(diǎn)

Dim xn1 As XmlNode

For Each xn1 In nls '遍歷

Dim xe2 As XmlElement = CType(xn1, XmlElement) '轉(zhuǎn)換類型

If xe2.Name = "Translation" Then '如果找到

xe2.InnerText ="替換"則修改

'Exit For Each '找到退出來(lái)就可以了

End If

Next xn1

Next xn

xmlDoc.Save("life.xml") '保存。

MessageBox.Show("修改XML成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)

看能不能幫到你!

網(wǎng)頁(yè)題目:vb.netxml修改的簡(jiǎn)單介紹
本文網(wǎng)址:http://www.muchs.cn/article0/hjeioo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、搜索引擎優(yōu)化網(wǎng)站導(dǎo)航、網(wǎng)站策劃、商城網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)

動(dòng)態(tài)網(wǎng)站知識(shí)