Build XML Document from recordset

By Allen Stoner

It's fairly easy to turn a recordset into an xml document by looping through the rows and fields.

    Dim xRec As New XmlDocument

'  Setupt the command object as needed here

      reader = command.ExecuteReader()

      Dim xNode As XmlNode
      Dim xAttr As XmlAttribute
      Dim Flds As Int16
      Dim I As Int16
      Dim xRow As XmlNode

      While reader.Read()
        Flds = reader.FieldCount
        xRow = xRec.CreateNode(XmlNodeType.Element, "", "Row", "")   Create a new row to include in the document
        For I = 0 To Flds - 1
          xNode = xRec.CreateNode(XmlNodeType.Element, "", "Data", "")   Create a field to include in the row
          xAttr = xRec.CreateAttribute("Name")    '  There is a name attribute that is the field name
          xAttr.Value = Trim(reader.GetName(I))
          xNode.InnerText = Trim(reader(I).ToString)  '  The text of the element contains the value
          xRow.AppendChild(xNode)    Append the field to the row
        xRec.DocumentElement.AppendChild(xRow)    Append the row to the document

      End While

Related FAQs

Many times when using XML and XSLT to transform it into an HTML document it is nice to put the output directly into the web browser control without writing it to a temporary file. This can be done by putting the transformation output into a memory stream and loading the web browser control from the memory stream.
Build XML Document from recordset  (594 Views)