JDEE8297
asked on
xml in asp.net 1.1 vb.net
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.Co lumnName)
Next
'Create the xml document, all xml with will have the first tag be <DATA>
xmleData = xmldDoc.CreateElement("DAT A")
'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("xml Row")
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(xmleR ow)
Next
'Add the data to xml document
xmldDoc.AppendChild(xmleDa ta)
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
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.Co
Next
'Create the xml document, all xml with will have the first tag be <DATA>
xmleData = xmldDoc.CreateElement("DAT
'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("xml
strFieldName = String.Empty
'Traverse through the field list and build the xml string
For Each strFieldName In arlFieldNames
With xmleRow
.SetAttribute(strFieldName
End With
strXml &= strFieldName & drRow(strFieldName) & "<br>"
Next
strXml &= "xmlerow" & xmleRow.InnerXml.ToString & xmleRow.InnerText.ToString
'Add the row to the data element
xmleData.AppendChild(xmleR
Next
'Add the data to xml document
xmldDoc.AppendChild(xmleDa
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
I am going to accept this. Gave me an idea on how to redo this. Thanks again.
ASKER
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.
ASKER
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.