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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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())
mitchellm44Author Commented:
Zero impact on the result - however the code improvement suggestion is well taken.
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
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

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?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.



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.
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)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.