[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

xml in asp.net 1.1  vb.net

Posted on 2007-11-29
5
Medium Priority
?
1,079 Views
Last Modified: 2010-04-21
Greetings,

I am trying to build an xml document from a datatable passed into it. I am making the function generic enough that it doesn't need to know the structure of the datatable or its field name and just return to me an xml string.

The variable is just my debugging variable, so it can be removed

 Public Function BuildXmlDoc(ByVal dtRecords As DataTable, ByRef strErrMsg As String) As String 'System.xml.XmlDocument
        'Local variables
        Dim xmldDoc As New System.xml.XmlDocument
        Dim xmleData As System.xml.XmlElement

        Dim drRow As DataRow
        Dim dcCol As DataColumn
        Dim arlFieldNames As New ArrayList
        Dim strFieldName As String = String.Empty
        Dim strXml As String
        'Build the xml string
        Try
            'Populate the field array list
            For Each dcCol In dtRecords.Columns
                arlFieldNames.Add(dcCol.ColumnName)
            Next
            'Create the xml document, all xml with will have the first tag be <DATA>
            xmleData = xmldDoc.CreateElement("DATA")
            'Now build each of the xml rows
            For Each drRow In dtRecords.Rows
                Dim xmleRow As System.xml.XmlElement
                strXml &= "dr<br>"
                'Set default values
                xmleRow = xmldDoc.CreateElement("xmlRow")
                strFieldName = String.Empty
                'Traverse through the field list and build the xml string
                For Each strFieldName In arlFieldNames
                    With xmleRow
                        .SetAttribute(strFieldName, drRow(strFieldName))
                    End With

                    strXml &= strFieldName & drRow(strFieldName) & "<br>"
                Next
                strXml &= "xmlerow" & xmleRow.InnerXml.ToString & xmleRow.InnerText.ToString & "<br>"
                'Add the row to the data element
                xmleData.AppendChild(xmleRow)
            Next
            'Add the data to xml document
            xmldDoc.AppendChild(xmleData)
        Catch ex As Exception
            strErrMsg = "cDataAccess.BuildXmlDoc: " & ex.Message & "<br />" & ex.StackTrace
        Finally
            'Clean up
            xmleData = Nothing
            arlFieldNames = Nothing
            dtRecords = Nothing
            dcCol = Nothing
            drRow = Nothing
        End Try
        'Return the value of the function
        Return "JRDxml " & strXml & "<br>" & xmldDoc.InnerText.ToString
    End Function

There is nothing coming back from here, and the variable xmleRow seems to have no data, and I am not sure why this is the case, because the datatable does get into it and no other errors are generated on the page. I copied this from another project that I had worked on before in my old work place, so I know it did work, just not sure why it doesn't work now.

Any help would be appreciated. Thanks.

JD
0
Comment
Question by:JDEE8297
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 37

Accepted Solution

by:
samtran0331 earned 375 total points
ID: 20376213
Is this SQL Server?
It kind of looks like what you're trying to do can be accomplished with SQL's "FOR XML"

...like (using Northwind):
SELECT * FROM Customers FOR XML AUTO

http://www.extremeexperts.com/SQL/Articles/ForXML.aspx
0
 

Author Comment

by:JDEE8297
ID: 20376393
true, it could be done in sql server.

However, what I am planning to do is to take the contents of a datatable and passed into a stored procedure via xml string, and then inside the stored procedure traverse through the xml data and update the respective table(s) without having to call the same stored procedure over and over.

0
 

Author Comment

by:JDEE8297
ID: 20379482
figured it out, I was calling the wrong property on the xmldocument variable, instead of innertext or innerxml, I should have called outerxml.

Probably would have found it sooner, but working on a page that does not user code behind, so it makes it kind cumbersome to debug. Otherwise, problem solved. Thanks for your help
0
 

Author Closing Comment

by:JDEE8297
ID: 31411741
I am going to accept this. Gave me an idea on how to redo this. Thanks again.
0
 

Author Comment

by:JDEE8297
ID: 20383692
samtran0331 thanks again, even though I figured out the error, your solution gave me another idea that I will try out on my app. Thanks again.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

649 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