利用Visual Basic操作XML数据
GetNodeValue = default_value
Else
GetNodeValue = value_node.Text
End If
End Function
´ Save the current values.
Private Sub SaveValues()
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode
´ Create the XML document.
Set xml_document = New DOMDocument
´ Create the Values section node.
Set values_node = xml_document.createElement("Values")
´ Add the Values section node to the document.
xml_document.appendChild values_node
´ Create nodes for the values inside the
´ Values section node.
CreateNode values_node, "FirstName", txtFirstName.Text
CreateNode values_node, "LastName", txtLastName.Text
CreateNode values_node, "Street", txtStreet.Text
CreateNode values_node, "City", txtCity.Text
CreateNode values_node, "State", txtState.Text
CreateNode values_node, "Zip", txtZip.Text
´ Save the XML document.
xml_document.save m_AppPath & "Values.xml"
End Sub
´ Add a new node to the indicated parent node.
Private Sub CreateNode(ByVal parent As IXMLDOMNode, _
ByVal node_name As String, ByVal node_value As String)
Dim new_node As IXMLDOMNode
´ Create the new node.
Set new_node = parent.ownerDocument.createElement(node_name)
´ Set the node´s text value.
new_node.Text = node_value
´ Add the node to the parent.
parent.appendChild new_node
End Sub
SaveValuesIndented 程序
虽然每个人都化了很大的精力去处理xml文档,使他们看上更容易些,但xml工具一般都忽略了那些使xml文档结构明显的空白和缩进,xml解析器也同样忽略缩进和空白。
不幸的是我们例子也同样忽略了这些缩进和空白,SaveValues创建了一个象下面那样的xml文件,所有的代码都在同一行中。
<Values><FirstName>Rod</FirstName><LastName>Stephens</LastNa
me><Street>1234 Programmer Place</Street><City>Bugsville</Ci
ty><State>CO</State><Zip>80276</Zip></Values>
VB.NET中包括了文本写入类,可以XML文档规定格式。但MSXML重没有这种功能,所以如果需要以一种清晰的格式保存XML文件,只能另行添加它的格式。
List2列出了程序SaveValuesIndented使用的代码,SaveValues子程序与上面例子中讲的几乎完全相同,但他在创建value节点后同时给XML文档创建了一个<value>标记的新行。
然后SaveValues 调用CreateNode创建一个新的数据节点,但在这里它传递给CreateNode一个新的参数,这个参数表示这个新节点的缩进方式。
CreateNode
´ Save the current values.
Private Sub SaveValues()
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode
´ Create the XML document.
Set xml_document = New DOMDocument
´ Create the Values section node.
Set values_node = xml_document.createElement("Values")
´ Add a new line.
values_node.appendChild xml_document.createTextNode(vbCrLf)
´ Add the Values section node to the document.
xml_document.appendChild values_node
´ Create nodes for the values inside the
´ Values section node.
CreateNode 4, values_nod
上一页 [1] [2] [3] [4] [5] 下一页
进入问吧