Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Consuming XML from a SQL server 2008 stored proc

Posted on 2013-10-22
2
Medium Priority
?
292 Views
Last Modified: 2013-10-23
I am looking for a good guide or article that demonstrates how to consume XML from a SQL server 2008 stored proceedure

A simplified version of the stored proc
SELECT    UserName
		,Forename
		,Surname
		,Email 
FROM dbo.UserDetails 
FOR XML RAW, ELEMENTS;

Open in new window


and a samle of the XML returned

<row>
  <UserName>preinvoiceca</UserName>
  <Forename>Detached Invoice</Forename>
  <Surname>Processor CA</Surname>
  <Email>preinvoiceca@trailers.com</Email>
</row>
<row>
  <UserName>preinvoiceusa</UserName>
  <Forename>Detached Invoice</Forename>
  <Surname>Processor USA</Surname>
  <Email>preinvoiceusa@trailers.com</Email>
</row>

Open in new window

0
Comment
Question by:Alyanto
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 39591562
Some examples with different techniques are posted in following link

http://developer.yahoo.com/dotnet/howto-xml_vb.html

You can also convert it to a dataset

http://www.nullskull.com/faq/1889/convert-an-xml-string-into-a-dataset.aspx


Public Shared Function XMLToDataSet(ByVal xmlStr As String, ByVal schemaFile As String) As DataSet
        'Convert the XML to a dataset
        Dim sr As New StringReader(xmlStr)

        'Convert xmlData to a Dataset
        Dim ds As New DataSet

        If schemaFile = String.Empty Then
            ds.ReadXml(sr, XmlReadMode.InferSchema)
        Else
            ds.ReadXmlSchema(schemaFile)
            ds.ReadXml(sr, XmlReadMode.ReadSchema)
        End If

        For Each relation As DataRelation In ds.Relations
            For Each c As DataColumn In relation.ParentColumns
                If Not relation.ChildTable.Columns.Contains(c.ColumnName) Then
                    relation.ChildTable.Columns.Add(c)
                End If
                For Each dr As DataRow In relation.ChildTable.Rows
                    dr(c.ColumnName) = dr.GetParentRow(relation)(c.ColumnName)
                Next
            Next
        Next

        Return ds
    End Function

Open in new window

0
 
LVL 1

Author Closing Comment

by:Alyanto
ID: 39593348
Spot on, and timely too :)

/Aly
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question