Only one top level element is allowed in an XML document -<< I dont see more than one

I am getting the Error
Only one top level element is allowed in an XML document,
which is produced from an ASP.NET page, within an AJAX interaction.

The code is identical to an earlier response - that works - the query is different.

The ASPX code writing the XML that fails:
/////////////////////////////////////////////////
(VB.NET)
 strSQL = "select * from tblAdminRights  "
       


        strXMLnames = New StringBuilder
        'strXMLnames.Append("<?xml version='1.0' encoding='ISO-8859-1'?>")
        Trace.Write(strSQL)
        dtOptions = FillDataTable(strSQL)
        Trace.Write("Row count = " & dtOptions.Rows.Count)
        If dtOptions.Rows.Count > 0 Then
            'strXMLnames.Append("<root>")
            strXMLnames.Append("<datalist>")
            For Each row In dtOptions.Rows
                strXMLnames.Append("<tblAdminRights  ")
                For Each field In dtOptions.Columns
                    strXMLnames.Append(" " & field.ColumnName & "=")
                    strXMLnames.Append("""" & Trim(row(field.ColumnName)) & """")
                Next
                strXMLnames.Append(" />")

            Next
            strXMLnames.Append("</datalist>")
            Trace.Write(strXMLnames.ToString())
        End If
        'Write XML to Client and have the Client Parse it
        Response.ContentType = "application/xml" '* Set Content Type
        Response.Write(strXMLnames.ToString())
//////////////////////////////////////////////////////////

I have changed the Content type between text/xml and application/xml -no luck.
I have added and removed wrapping tags from other posts I saw - no luck
I have also put  an XML Tag in the front - with no luck.(commented out in code above)

I can capture the XML being written out from the Trace.Write before the last Endif - and open it in IE and it displays correctly - but the XML from the server gets the error.
The ONLY difference between this code and code that works - is the way the SQL is built -tablename for the output node. The attributes are output identical.
and it works.


///////////////// THE   XML  OUTPUT ////////////////////

<datalist>
<tblAdminRights   RACFID="U8MM3" CanEdit="True" Name="UserName1" Administrator="True" />
<tblAdminRights   RACFID="UGDT111" CanEdit="True" Name="UserName2" Administrator="True" />
</datalist>

////////////////////////////////////////


I am just not seeing the error.  I need someone to help me see what is in error here..

LVL 2
mitchellm44Asked:
Who is Participating?
 
TheMegaLoserCommented:
Are you perhaps adding the result to a string that you dont reset? Thereby getting two root elements with the same name?

If not, can we access and see the page with the error? Testdrive the data fetching URL?
0
 
YZlatCommented:
try

strSQL = "select * from tblAdminRights  "
       


        strXMLnames = New StringBuilder
        'strXMLnames.Append("<?xml version='1.0' encoding='ISO-8859-1'?>")
        Trace.Write(strSQL)
        dtOptions = FillDataTable(strSQL)
        Trace.Write("Row count = " & dtOptions.Rows.Count)
  'strXMLnames.Append("<root>")
            strXMLnames.Append("<datalist>")

        If dtOptions.Rows.Count > 0 Then
                      For Each row In dtOptions.Rows
                strXMLnames.Append("<tblAdminRights  ")
                For Each field In dtOptions.Columns
                    strXMLnames.Append(" " & field.ColumnName & "=")
                    strXMLnames.Append("""" & Trim(row(field.ColumnName)) & """")
                Next
                strXMLnames.Append(" />")

            Next
                   End If
 strXMLnames.Append("</datalist>")
            Trace.Write(strXMLnames.ToString())

        'Write XML to Client and have the Client Parse it
        Response.ContentType = "application/xml" '* Set Content Type
        Response.Write(strXMLnames.ToString())
0
 
mitchellm44Author Commented:
Zero impact on the result - however the code improvement suggestion is well taken.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
mitchellm44Author Commented:
In looking for an answer -- I saw a function to convert an ADO Recordset object, into an XML String using the
XMLDOM object.

It reminded me of the .Net built in classes for XML and so I redefined this function to use a Datatable

It works with One table and not with the other - just as before and gauranteed same code

So what is causing the Object errror - is there something in the data that is causing this?

//////////////////////////////////////////////////////////

    Function ConvertDTtoXML(ByVal oDT As DataTable, ByVal strTopLevelNodeName As String, ByVal strRowNodeName As String) As String

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ''Function:  ConvertDTToXML()
        ''Parameters:
        ''  oDT:  The ADO DataTable object
        ''  strTopLevelNodeName: The descriptive name for the top-level node (E.g. customers)
        ''  strRowNodeName: The descriptive name for the nodes (E.g. customer)
        ''Return:
        ''  The XML string.
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim oXMLDoc As XmlDocument
        Dim oXMLRoot As XmlNode
        Dim oXMLNode As XmlNode
        Dim oAttributes As XmlAttributeCollection
        Dim oAttr As XmlAttribute

        Dim oRow As DataRow
        Dim oField As DataColumn
        Dim ix As Integer
        '** Create the start of the document
        oXMLDoc = New XmlDocument
        oXMLNode = oXMLDoc.CreateXmlDeclaration("1.0", "", "")
        oXMLDoc.AppendChild(oXMLNode)
        oXMLRoot = oXMLDoc.CreateElement(strTopLevelNodeName)


        If oDT.Rows.Count > 0 Then
            For Each oRow In oDT.Rows
                oXMLNode = oXMLDoc.CreateElement(strRowNodeName)
                For Each oField In oDT.Columns
                    oAttr = oXMLDoc.CreateAttribute(oField.ColumnName)
                    oAttr.Value = oRow(oField.ColumnName) & "" '*In case of NULLS
                    oXMLNode.Attributes.Append(oAttr)
                Next
                oXMLRoot.AppendChild(oXMLNode)
            Next
        End If

        '// Append the root and its children to the Document
        oXMLDoc.AppendChild(oXMLRoot)

        Return oXMLDoc.OuterXml
    End Function
0
 
mitchellm44Author Commented:
The site is only an Intranet site.. so it is not visble to the outside world.

There is only ONE Response.Write()

However,as an Experiment, I commented out the write ... and saw that the DEBUG for the ASP.NET Page being set to true
was writing out Debug info.   I turned this off - and my output showed up.

My recollection is that I was able to have the debug on before and it came through as part of the response.. but this time
it choked

So instead of Mega loser  today you are Mega Point winner.



0
 
mitchellm44Author Commented:
One other thing, Mega Winner.. if you got here from another link, Make sure you go back to that point and re-read the instructions.
0
 
TheMegaLoserCommented:
Made a comment on the link I followed.

Btw,  MegaLoser is a very good nick, for some reason it's never taken when you want to register on a site. Can't imagine why ;) (hope nobody starts using it now)

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.