您现在的位置是: 软件 > 开发者网络 > 程序方舟 > 开发专栏 > VB开发 > 正文
·速成电脑精英(包分配)白领高薪一族从这里开始



-Java套接字编程(下)
-MediaStudio Pro 6.5教程
-三款卸载软件最新试用
-基于Visual C++的Winsock API研究

在Visual Basic 6.0中操纵XML文件
2002-10-17· ·crystal编译··yesky

上一页  1 2 3  

  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_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

' 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编程的表面,本文的例子中的涉及只是非常简单的XML文件,但你可以使用使用本文揭示的技术做更多的事情,比如配置设置、表单位置、以及其他信息。XML已经向前更进一步的发展了,有了更复杂的数据层次。对于更复杂的数据结构,在运行时可以更容易的使用MSXML对象来存取XML文件

上一页  1 2 3  

【责任编辑:方舟】
【发表评论】【关闭窗口】
■ 相关内容
 Visual Basic .NET实现后台处理(下 )
 Visual Basic .NET实现后台处理(上)
 运用VB.net创建Web服务访问程序
 Visual Basic .NET中操作MsAgent
 Visual Basic.NET快速开发MIS系统
 Windows 98/2000下多屏显示技术的实现
 Visual Basic中实现I/O端口控制
 Visual Basic.NET和GDI+共创图标编辑器
 在Visual Basic .NET中使用存储过程
 一步一步创建Visual Basic .NET 控件
 Visual Basic 图形及图像处理
 Visual Basic文件操作处理专辑
 Visual Basic .NET 中的异常处理简介
 Visual Basic 控件及技巧编程
 Visual Basic 硬件与系统设计
 Visual Basic 游戏开发
 Visual Basic 界面设计大观
感谢 访问天极网,如果您觉得该文章涉及版权问题,请看这里!