Dim m_AppPath As String
Private Sub Form_Load()
' Get the application's startup path.
m_AppPath = App.Path
If Right$(m_AppPath, 1) <> "\" Then m_AppPath = m_AppPath & "\"' Load the values.
LoadValues
End SubPrivate Sub Form_Unload(Cancel As Integer)
' Save the current values.
SaveValues
End Sub' Load saved values from XML.
Private Sub LoadValues()
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode' Load the document.
Set xml_document = New DOMDocument
xml_document.Load m_AppPath & "Values.xml"' If the file doesn't exist, then
' xml_document.documentElement is Nothing.
If xml_document.documentElement Is Nothing Then
' The file doesn't exist. Do nothing.
Exit Sub
End If' Find the Values section.
Set values_node = xml_document.selectSingleNode("Values")' Read the saved values.txtFirstName.Text = GetNodeValue(values_node, "FirstName", "???")
txtLastName.Text = GetNodeValue(values_node, "LastName", "???")
txtStreet.Text = GetNodeValue(values_node, "Street", "???")
txtCity.Text = GetNodeValue(values_node, "City", "???")
txtState.Text = GetNodeValue(values_node, "State", "???")
txtZip.Text = GetNodeValue(values_node, "Zip", "???")
End Sub' Return the node's value.
Private Function GetNodeValue(ByVal start_at_node As IXMLDOMNode, _
ByVal node_name As String, _
Optional ByVal default_value As String = "") As String
Dim value_node As IXMLDOMNodeSet value_node = start_at_node.selectSingleNode(".//" & node_name)
If value_node Is Nothing Then
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 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_node, "FirstName", txtFirstName.Text
CreateNode 4, values_node, "LastName", txtLastName.Text
CreateNode 4, values_node, "Street", txtStreet.Text
CreateNode 4, values_node, "City", txtCity.Text
CreateNode 4, values_node, "State", txtState.Text
CreateNode 4, values_node, "Zip", txtZip.Text
xml_document.createAttribute "lll"
' 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 indent As Integer, _
ByVal parent As IXMLDOMNode, ByVal node_name As String, _
ByVal node_value As String)
Dim new_node As IXMLDOMNode' Indent.
parent.appendChild _
parent.ownerDocument.createTextNode(Space$(indent))' 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' Add a new line.
parent.appendChild parent.ownerDocument.createTextNode(vbCrLf)
End Sub建立后的XML:
- <Values>
<FirstName>Text1</FirstName>
<LastName>Text2</LastName>
<Street>Text3</Street>
<City>Text4</City>
<State>Text5</State>
<Zip>Text6</Zip>
</Values>
而我想要的XML是
- <Values>
<FirstName>Text1</FirstName>
<LastName>Text2</LastName>
-<input xsi:type="avFileInput">
<Street>Text3</Street>
</input>
<City>Text4</City>
<State>Text5</State>
<Zip>Text6</Zip>
</Values
谁可以帮助改改!
Private Sub Form_Load()
' Get the application's startup path.
m_AppPath = App.Path
If Right$(m_AppPath, 1) <> "\" Then m_AppPath = m_AppPath & "\"' Load the values.
LoadValues
End SubPrivate Sub Form_Unload(Cancel As Integer)
' Save the current values.
SaveValues
End Sub' Load saved values from XML.
Private Sub LoadValues()
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode' Load the document.
Set xml_document = New DOMDocument
xml_document.Load m_AppPath & "Values.xml"' If the file doesn't exist, then
' xml_document.documentElement is Nothing.
If xml_document.documentElement Is Nothing Then
' The file doesn't exist. Do nothing.
Exit Sub
End If' Find the Values section.
Set values_node = xml_document.selectSingleNode("Values")' Read the saved values.txtFirstName.Text = GetNodeValue(values_node, "FirstName", "???")
txtLastName.Text = GetNodeValue(values_node, "LastName", "???")
txtStreet.Text = GetNodeValue(values_node, "Street", "???")
txtCity.Text = GetNodeValue(values_node, "City", "???")
txtState.Text = GetNodeValue(values_node, "State", "???")
txtZip.Text = GetNodeValue(values_node, "Zip", "???")
End Sub' Return the node's value.
Private Function GetNodeValue(ByVal start_at_node As IXMLDOMNode, _
ByVal node_name As String, _
Optional ByVal default_value As String = "") As String
Dim value_node As IXMLDOMNodeSet value_node = start_at_node.selectSingleNode(".//" & node_name)
If value_node Is Nothing Then
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 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_node, "FirstName", txtFirstName.Text
CreateNode 4, values_node, "LastName", txtLastName.Text
CreateNode 4, values_node, "Street", txtStreet.Text
CreateNode 4, values_node, "City", txtCity.Text
CreateNode 4, values_node, "State", txtState.Text
CreateNode 4, values_node, "Zip", txtZip.Text
xml_document.createAttribute "lll"
' 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 indent As Integer, _
ByVal parent As IXMLDOMNode, ByVal node_name As String, _
ByVal node_value As String)
Dim new_node As IXMLDOMNode' Indent.
parent.appendChild _
parent.ownerDocument.createTextNode(Space$(indent))' 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' Add a new line.
parent.appendChild parent.ownerDocument.createTextNode(vbCrLf)
End Sub建立后的XML:
- <Values>
<FirstName>Text1</FirstName>
<LastName>Text2</LastName>
<Street>Text3</Street>
<City>Text4</City>
<State>Text5</State>
<Zip>Text6</Zip>
</Values>
而我想要的XML是
- <Values>
<FirstName>Text1</FirstName>
<LastName>Text2</LastName>
-<input xsi:type="avFileInput">
<Street>Text3</Street>
</input>
<City>Text4</City>
<State>Text5</State>
<Zip>Text6</Zip>
</Values
谁可以帮助改改!
后面加入CreateNode 4, values_node, "</input>", ""
应该就可以了
Dim input_node As IXMLDOMElement
Set input_node = values_node.appendChild(xml_document.createElement("input"))
input_node.setAttribute "xsi:type", "avFileInput"
CreateNode 4, input_node, "Street", txtStreet.Text