vb.net Visual basic .net XML ( IT Farmer : Visual Basic & XML)
XML wiki ::: XML 系列教程(w3school CN) :
How to XML in VB.NET (Tutorial and source Code) : http://vb.net-informations.com/xml/how-to-xml-in-vb.net.htm
Programming tutorials and source code examples (VB.Net by API) - XmlDocument
==========================================================
■■■ System.Xml(NameSpace) ::: System.Xml ::: XmlNodeType
■■ System.Xml.XmlDocument :
■ 建構 :
■ 屬性 : DocumentElement :::
■ 方法 : Load(Stream) ::: Load(String) ::: Load(TextReader) ::: Load(XmlReader) ::: SelectNodes(String) ::: SelectSingleNode(String) :::
■■ System.Xml.XmlNode : mNode.Attributes.ItemOf("field").Value :::
■ 建構 :
■ 屬性 : FirstChild ::: LastChild ::: HasChildNodes ::: ChildNodes ::: Attributes :::
■ 方法 :
■■ System.Xml.XmlNodeList :
■■ System.Xml.XmlNamedNodeMap :
■■ System.Xml.XmlElement : ChildNodes :::
Dim root As XmlNode = xmlDoc.DocumentElement
mXmlElm = root.SelectSingleNode("//HMO_MEBERSHIP_UPDATE/COMMAND")
■■ System.Xml.XmlText :
■■ System.Xml.XPath : [XPath Wiki] ::: [IT 選擇節點語法] ::: [XPath 教程-實例] ::: [W3school XPath]
■ 建構 :
■ 屬性 :
■ 方法 :
■■ System.Xml.Schema :
==========================================================
■■■ a
==========================================================
■■■ Write Xml To File test.xml ( Source Code 參考原紿網站: - XmlDocument )
Dim myDoc As New Xml.XmlDocument
myDoc.AppendChild(myDoc.CreateXmlDeclaration("1.0", "UTF-8", String.Empty))
myDoc.AppendChild(myDoc.CreateComment("information about this book"))
Dim rootBook As Xml.XmlNode = myDoc.AppendChild(myDoc.CreateElement("book"))
rootBook.Attributes.Append(myDoc.CreateAttribute("isbn"))
rootBook.Attributes("isbn").Value = "0-672-32549-7"
Dim title As Xml.XmlNode
title = rootBook.AppendChild(myDoc.CreateElement("title"))
title.InnerText = "titleStart"
Dim authors As Xml.XmlNode = rootBook.AppendChild(myDoc.CreateElement("authors"))
Dim Duncan, Andy, Erik, Joel As Xml.XmlNode
Duncan = authors.AppendChild(myDoc.CreateElement("author"))
Andy = authors.AppendChild(myDoc.CreateElement("author"))
Erik = authors.AppendChild(myDoc.CreateElement("author"))
Joel = authors.AppendChild(myDoc.CreateElement("author"))
Duncan.InnerText = "authorD"
Andy.InnerText = "authorA"
Erik.InnerText = "authorE"
Joel.InnerText = "authorJ"
Dim chapters As Xml.XmlNode = myDoc.CreateElement("chapters")
rootBook.AppendChild(chapters)
Dim chapterElements(10) As Xml.XmlElement
For i As Integer = 0 To 10
chapterElements(i) = myDoc.CreateElement("chapter")
With chapterElements(i)
.Attributes.Append(myDoc.CreateAttribute("id"))
.Attributes("id").Value = CStr(i + 1)
.Attributes.Append(myDoc.CreateAttribute("topic"))
End With
chapters.AppendChild(chapterElements(i))
Next
chapterElements(0).Attributes("topic").Value = "topA"
chapterElements(1).Attributes("topic").Value = "topB"
chapterElements(2).Attributes("topic").Value = "topC"
chapterElements(3).Attributes("topic").Value = "topD"
chapterElements(4).Attributes("topic").Value = "topE"
chapterElements(5).Attributes("topic").Value = "topF"
chapterElements(6).Attributes("topic").Value = "topG"
chapterElements(7).Attributes("topic").Value = "topH"
chapterElements(8).Attributes("topic").Value = "topI"
chapterElements(9).Attributes("topic").Value = "topJ"
chapterElements(10).Attributes("id").Value = "topK"
chapterElements(10).Attributes("topic").Value = "topL"
Dim myWriter As New Xml.XmlTextWriter("test.xml", System.Text.Encoding.UTF8)
myWriter.Formatting = Xml.Formatting.Indented
myDoc.Save(myWriter)
myWriter.Close()
MsgBox("Write test.xml ok")
==========================================================
■■■ XML SelectNodes Demo Code [ test.xml] txtMessage Is a textBox or richTextBox
Dim myDoc As New Xml.XmlDocument
myDoc.Load("test.xml")
txtMessage.Text = ""
Dim chpts As Xml.XmlNodeList = myDoc.SelectNodes("/book/chapters/chapter")
For Each nd As Xml.XmlNode In chpts
txtMessage.Text &= nd.Attributes("id").Value & " -- "
Next
txtMessage.Text &= vbNewLine
Dim book As Xml.XmlElement
book = myDoc.Item("book")
txtMessage.Text &= "ISBN: " & book.Attributes("isbn").Value & vbNewLine
Dim title As Xml.XmlElement
title = book.Item("title")
txtMessage.Text &= title.InnerText & vbNewLine
txtMessage.Text &= "Authors:"
Dim authors As Xml.XmlElement
authors = book.Item("authors")
For Each node As Xml.XmlNode In authors.ChildNodes
If node.Name = "author" Then
txtMessage.Text &= node.InnerText & " , "
End If
Next
txtMessage.Text &= vbNewLine
Dim chapters As Xml.XmlElement
chapters = book.Item("chapters")
For Each node As Xml.XmlNode In chapters.ChildNodes
If node.Name = "chapter" Then
txtMessage.Text &= node.Attributes("id").Value & " --- "
txtMessage.Text &= node.Attributes("topic").Value & vbNewLine
End If
Next
==========================================================
■■■ Demo OutPut in textBox
1 -- 2 -- 3 -- 4 -- 5 -- 6 -- 7 -- 8 -- 9 -- 10 -- topK --
ISBN: 0-672-32549-7
titleStart
Authors:authorD , authorA , authorE , authorJ ,
1 --- topA
2 --- topB
3 --- topC
4 --- topD
5 --- topE
6 --- topF
7 --- topG
8 --- topH
9 --- topI
10 --- topJ
topK --- topL
==========================================================
■■■ text.xml
<?xml version="1.0" encoding="utf-8"?>
<!--information about this book-->
<book isbn="0-672-32549-7">
<title>titleStart</title>
<authors>
<author>authorD</author>
<author>authorA</author>
<author>authorE</author>
<author>authorJ</author>
</authors>
<chapters>
<chapter id="1" topic="topA" />
<chapter id="2" topic="topB" />
<chapter id="3" topic="topC" />
<chapter id="4" topic="topD" />
<chapter id="5" topic="topE" />
<chapter id="6" topic="topF" />
<chapter id="7" topic="topG" />
<chapter id="8" topic="topH" />
<chapter id="9" topic="topI" />
<chapter id="10" topic="topJ" />
<chapter id="topK" topic="topL" />
</chapters>
</book>
==========================================================
■■■ a
==========================================================
■■■ a
■ 建構 :
■ 屬性 :
■ 方法 :
a
vb.net Visual basic .net XML
on
0 意見:
張貼留言