[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-03-31
7
Medium Priority
?
1,549 Views
Last Modified: 2013-11-19
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..

0
Comment
Question by:mitchellm44
  • 4
  • 2
7 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 16346717
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
 
LVL 2

Author Comment

by:mitchellm44
ID: 16350027
Zero impact on the result - however the code improvement suggestion is well taken.
0
 
LVL 2

Author Comment

by:mitchellm44
ID: 16361947
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
TheMegaLoser earned 2000 total points
ID: 16362890
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
 
LVL 2

Author Comment

by:mitchellm44
ID: 16363341
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
 
LVL 2

Author Comment

by:mitchellm44
ID: 16363515
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
 
LVL 12

Expert Comment

by:TheMegaLoser
ID: 16363647
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

873 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