如何从数据库读取数据组织成xml文件发送出去

2024-12-18 18:16:35
推荐回答(3个)
回答1:

VB的代码: Public Sub SaveValues()
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMElement
Dim values_chilnode As IXMLDOMElement
Dim values_thrnode As IXMLDOMElement
Dim str As String
Dim strsql As String
Dim i As Integer Dim rs As ADODB.Recordset
strsql = "select StudyTypeID,Title,enable,eMod from StudyType "
Set rs = gconn.Execute(strsql)
Set xml_document = New DOMDocument
'创建根节点
Set values_node = CreateRootNode(xml_document, "root")
'创建词根节点
Set values_chilnode = CreateChildNode(xml_document, values_node, "user")
'循环rs记录集
While Not rs.EOF
'再次创建词根节点
Set values_thrnode = CreateChildNode(xml_document, values_node, "userinfo")
'子节点加入父节点
AddElementToParent xml_document, values_thrnode, "StudyTypeID", rs("StudyTypeID")
AddElementToParent xml_document, values_thrnode, "Title", rs("Title")
AddElementToParent xml_document, values_thrnode, "enable", rs("enable")
AddElementToParent xml_document, values_thrnode, "eMod", rs("eMod")
xml_document.Save (App.Path & "\vb.xml")
rs.MoveNextWendMsgBox (" 已成功保存到跟目录vb.xml中")End Sub'创建根节点的方法
Public Function CreateRootNode(xmlDoc As DOMDocument, RootNodeName As String) As IXMLDOMElement
Dim objRootNode As IXMLDOMElement

Set objRootNode = xmlDoc.createNode("element", RootNodeName, "")
xmlDoc.appendChild objRootNode

Set CreateRootNode = objRootNode
End Function
'创建子节点的方法
Public Function CreateChildNode(xmlDoc As DOMDocument, ParentNode As IXMLDOMElement, NewNodeName As String) As IXMLDOMElement
Dim NewNode As IXMLDOMElement
Dim NewNode1 As IXMLDOMElement
Set NewNode = xmlDoc.createNode("element", NewNodeName, "")
ParentNode.appendChild NewNode
Set CreateChildNode = NewNodeEnd Function
'子节点加入父节点并读取值
Public Sub AddElementToParent(xmlDoc As DOMDocument, elemParent As IXMLDOMElement, elemName As String, sValue As String)
Dim ChildNode As IXMLDOMElement

Set ChildNode = xmlDoc.createNode("element", elemName, "")
ChildNode.Text = sValue
elemParent.appendChild ChildNode
Set ChildNode = Nothing
End Sub

回答2:

不知道你用的是什么语言,以asp为例,最简单的办法就是

Response.Write("")

其实,你对这个理解是有误的,xml并不是发送,而是被请求,通过xmlhttp方式。
比如,你组织xml的那个文件叫做,makexml.asp

那你的请求端,通过get方式访问makexml.asp,获得Response.Write的xml,是最简单直观的一种方式。

当然,C#本身有data2xml,其他很多语言都有直接的控件。希望你能问的更清楚一点。

回答3:

将表数据保存到DataSet ,然后 ...用DataSet.WriteToXxml 方法 将数据 存到 xml 就可以....