Ehab Developer
Ehab Developer

Reputation: 11

This document already has a 'DocumentElement' node -Exception

I get this Exception when in try to convert data relation from Data set tables to xml to make an XmlDataSource supply it to Dynamic Menu Control.

This is the Code behind

 DataSet ds = new DataSet();

        SqlCommand cmdmenu = new SqlCommand("select CategoryID,CategoryName from Category ", ConManager.Con());

        SqlCommand com2 = new SqlCommand("select CategoryIDToSub,subcategoryid ,SubCategoryName from CategorySub", ConManager.Con());
        SqlCommand cmd3 = new SqlCommand("select subcategoryid 'SubCategoryID2',Sub2CategoryName from CategorySub2", ConManager.Con());

        SqlDataAdapter menadapter = new SqlDataAdapter(cmdmenu.CommandText, ConManager.Con());
        SqlDataAdapter menadapter2 = new SqlDataAdapter(com2.CommandText, ConManager.Con());
        SqlDataAdapter menadapter3 = new SqlDataAdapter(cmd3.CommandText, ConManager.Con());
        menadapter.Fill(ds, "Menu");

        menadapter.SelectCommand = com2;
        menadapter2.Fill(ds, "SubMenu");
        menadapter3.SelectCommand = cmd3;
        menadapter3.Fill(ds, "SubSubMenu");

        DataColumn colParent =
        ds.Tables["Menu"].Columns["CategoryID"];
        DataColumn colChild =
        ds.Tables["SubMenu"].Columns["CategoryIDToSub"];

        DataRelation relation = new DataRelation("relationName",
        colParent,
        colChild, true);
        DataRelation Relation2 = new DataRelation("relationName2",
        ds.Tables["SubMenu"].Columns["subcategoryid"],
        ds.Tables["SubSubMenu"].Columns["SubCategoryID2"], true);

        relation.Nested = true;
        Relation2.Nested = true;

        ds.Relations.Add(relation);
        ds.Relations.Add(Relation2);

        XmlDataSource.Data = ds.GetXml();

        if (Request.Params["Sel"] != null)
            Page.Controls.Add(new System.Web.UI.LiteralControl("You selected " + Request.Params["Sel"]));

and this is the Xslt File Code

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" encoding="utf-8"/>


  <!-- Replace root node name Menus with MenuItems
       and call MenuListing for its children-->
  <xsl:template match="/Menus">
    <MenuItems>
      <xsl:call-template name="MenuListing" />
    </MenuItems>
  </xsl:template>

  <!-- Allow for recursive child nodeprocessing -->
  <xsl:template name="MenuListing">
    <xsl:apply-templates select="Menu" />
  </xsl:template>

  <xsl:template match="Menu">
    <MenuItem>
      <!-- Convert Menu child elements to MenuItem attributes -->
      <xsl:attribute name="Text">
        <xsl:value-of select="CategoryName"/>
      </xsl:attribute>
      <xsl:attribute name="ToolTip">
        <xsl:value-of select="Remarks"/>
      </xsl:attribute>
      <xsl:attribute name="NavigateUrl">
        <xsl:text>?Sel=</xsl:text>
        <xsl:value-of select="Text"/>
      </xsl:attribute>
      <xsl:attribute name="Text2">
        <xsl:value-of select="SubCategoryName"/>
      </xsl:attribute>
      <xsl:attribute name="Test3">
        <xsl:value-of select="Sub2CategoryName3"/>
      </xsl:attribute>
      <!-- Recursively call MenuListing forchild menu nodes -->
      <xsl:if test="count(Menu) >0">
        <xsl:call-template name="MenuListing" />
      </xsl:if>
    </MenuItem>
  </xsl:template>
</xsl:stylesheet>

and this is the Menu Control and the XmlDataSource Code

<asp:Menu ID="Menu" runat="server" BackColor="#E3EAEB"   DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#666666" StaticSubMenuIndent="10px" DataSourceID="XmlDataSource" Width="225px" onmenuitemclick="Menu_MenuItemClick">

<DataBindings>
<asp:MenuItemBinding DataMember="MenuItem" NavigateUrlField="NavigateUrl" TextField="Text" />
<asp:MenuItemBinding DataMember="MenuItem" NavigateUrlField="NavigateUrl" TextField="Text2" />
<asp:MenuItemBinding DataMember="MenuItem" NavigateUrlField="NavigateUrl" TextField="Text3" />
</DataBindings>

            <StaticSelectedStyle BackColor="#1C5E55" />
            <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <DynamicHoverStyle BackColor="#666666" ForeColor="White" />
            <DynamicMenuStyle BackColor="#E3EAEB" />
            <DynamicSelectedStyle BackColor="#1C5E55" />
            <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <StaticHoverStyle BackColor="#666666" ForeColor="White" />
      </asp:Menu>                       
      <asp:XmlDataSource ID="XmlDataSource" runat="server"
            TransformFile="~/TransformXSLT.xsl" XPath="MenuItems/MenuItem">
      </asp:XmlDataSource>

Upvotes: 0

Views: 12553

Answers (1)

Richard Schneider
Richard Schneider

Reputation: 35477

Your XML document is not well formed. You have more than one document (root) element. For example:

<a>some</a>
<a>thing</a>

is not well formed. Whereas:

<list>
  <a>some</a>
  <a>thing</a>
</list>

is well formed.

Upvotes: 4

Related Questions