2007-02-22

Operate XML

載入檔案
Dim XMLFileName As String = "File name here"
XMLDoc As New Xml.XmlDocument
XMLDoc.Load(XMLFileName)
 
取得節點
XMLNode As Xml.XmlNode
XMLNode = XMLDoc.SelectSingleNode("MakeMoneySystem/ShippingLabel")
p.s 如果node不存在, XMLNode=Nothing
 
新增節點
Dim root As Xml.XmlNode = XMLDoc.DocumentElement
Dim elem As Xml.XmlElement = XMLDoc.CreateElement("節點名稱")
elem.SetAttribute("屬性名稱", "值")
elem.InnerText = "節點值"
root.AppendChild(elem)
XMLDoc.Save(XMLFileName)
 
取得屬性值
XMLNode.Attributes.ItemOf("屬性名稱").Value
 
新增屬性
Dim NewAttr As Xml.XmlAttribute = XMLDoc.CreateAttribute("屬性名稱")
NewAttr.Value = "屬性值"
XMLNode.Attributes.SetNamedItem(NewAttr)
XMLDoc.Save(XMLFileName)

沒有留言:

張貼留言