该文档已经有一个'DocumentElement'节点-Exception [英] This document already has a 'DocumentElement' node -Exception

查看:85
本文介绍了该文档已经有一个'DocumentElement'节点-Exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
尝试将数据集表中的数据关系转换为xml,以使XmlDataSource将其提供给动态菜单控件时,出现此异常.

这是背后的代码

Hello
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"]));

,这是Xslt文件代码

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>


这是菜单控件和XmlDataSource代码


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:XmlDataSource ID="XmlDataSource" runat="server" 

        TransformFile="~/TransformXSLT.xsl" XPath="MenuItems/MenuItem">



plz帮助
请帮忙.



plz help
Please, help.

推荐答案

我建​​议您的数据不符合您的期望,因此正在尝试创建两个根元素.为什么不将SQL放在数据层中,并编写自己的代码以生成XML?
I would suggest your data does not match as you hope, and thus is trying to create two root elements. Why not put your SQL in a data layer, and write your own code to generate your XML ?


你好ehab,

解决方案是
替换
hello ehab,

The solution is to
replace
DataSet ds = new DataSet();



with



with

DataSet ds = new DataSet("Menus");



问题是:
如果默认情况下不提供数据集名称,则其name属性将为NewDataSet ,这是XSL无法找到Menus 根节点的位置,因此结果如下:



The problem is:
if you don''t supply dataset a name by default it''s name property will be NewDataSet and this is where your XSL is not able to find Menus root node hence below result:

<?xml version="1.0" encoding="utf-8"?>
  <MenuItem Text="Category Name 1" ToolTip="" NavigateUrl="?Sel=" Text2="" Test3="" />
  <MenuItem Text="Category Name 2" ToolTip="" NavigateUrl="?Sel=" Text2="" Test3="" />
  <MenuItem Text="Category Name 3" ToolTip="" NavigateUrl="?Sel=" Text2="" Test3="" />
  <MenuItem Text="Category Name 4" ToolTip="" NavigateUrl="?Sel=" Text2="" Test3="" />
  <MenuItem Text="Category Name 5" ToolTip="" NavigateUrl="?Sel=" Text2="" Test3="" />



您会看到找到了所有MenuItem,但由于根节点不匹配,因此会导致XML无效,即多个根元素.

希望对您有帮助.

谢谢,
Hemant



you see the all MenuItem are found but because the root node is a mismatch this results in invalid XML i.e. multiple root elements.

hope this will help you.

Thanks,
Hemant


这是一个单独的解决方案,用于解决您在第一个解决方案中提出的其他问题.一个请求,请为单独的问题添加单独的问题,这有助于其他人轻松找到类似问题的答案.

现在您面临的新问题是:
如果将XSL应用于XML,则从ds.GetXml()获得.您将获得以下输出:
This is a seperate solution for seperate problem you''ve asked in my first solution. A request please add seperate question for seperate problem it helps other to easily locate answer for similar problem.

Now the new problem you are facing:
if you apply XSL to the XML you are getting from ds.GetXml(); you will get below output:
<?xml version="1.0" encoding="utf-8"?>
<MenuItems>
  <MenuItem Text="Category Name 1" ToolTip="" NavigateUrl="?Sel=" Text2="" Test3="" />
  <MenuItem Text="Category Name 2" ToolTip="" NavigateUrl="?Sel=" Text2="" Test3="" />
  <MenuItem Text="Category Name 3" ToolTip="" NavigateUrl="?Sel=" Text2="" Test3="" />
  <MenuItem Text="Category Name 4" ToolTip="" NavigateUrl="?Sel=" Text2="" Test3="" />
  <MenuItem Text="Category Name 5" ToolTip="" NavigateUrl="?Sel=" Text2="" Test3="" />
</MenuItems>



如果树状菜单没有任何树状XML,则应更新XSL以为XMLDatasource生成适当的XML.我已经对XSL进行了一些更新,您可以根据您的应用要求对其进行修改.

XSL:



you do not have any tree-like XML for a tree-like menu, you should update your XSL to generate appropriate XML for XMLDatasource. I have updated the XSL a bit and you can modify it according to your app requirement.

XSL:

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

    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <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:call-template name="Menu"/>
      <!-- Recursively call MenuListing forchild menu nodes -->
      <xsl:if test="count(Menu) >0">
        <xsl:call-template name="MenuListing" />
      </xsl:if>
    </MenuItem>
  </xsl:template>
  <!-- Template for sub menu item -->
  <xsl:template name="Menu">
    <xsl:apply-templates select="SubMenu" />
  </xsl:template>
  <xsl:template match="SubMenu">
    <SubMenuItem>
      <!-- Convert Menu child elements to MenuItem attributes -->
      <xsl:attribute name="Text">
        <xsl:value-of select="SubCategoryName"/>
      </xsl:attribute>
      <xsl:attribute name="NavigateUrl">
        <xsl:text>?Sel=</xsl:text>
        <xsl:value-of select="Text"/>
      </xsl:attribute>
      <xsl:call-template name="SubMenu"/>
      <!-- Recursively call Menu forchild submenu nodes -->
      <xsl:if test="count(SubMenu) >0">
        <xsl:call-template name="Menu" />
      </xsl:if>
    </SubMenuItem>
  </xsl:template>
  <!-- Template for subsub menu item -->
  <xsl:template name="SubMenu">
    <xsl:apply-templates select="SubSubMenu" />
  </xsl:template>
  <xsl:template match="SubSubMenu">
    <SubSubMenuItem>
      <!-- Convert Menu child elements to MenuItem attributes -->
      <xsl:attribute name="Text">
        <xsl:value-of select="Sub2CategoryName"/>
      </xsl:attribute>
      <xsl:attribute name="NavigateUrl">
        <xsl:text>?Sel=</xsl:text>
        <xsl:value-of select="Text"/>
      </xsl:attribute>
      <!-- Recursively call Menu forchild submenu nodes -->
      <xsl:if test="count(SubSubMenu) >0">
        <xsl:call-template name="SubMenu" />
      </xsl:if>
    </SubSubMenuItem>
  </xsl:template>
</xsl:stylesheet>



The above XSL will produce below result:



The above XSL will produce below result:

<?xml version="1.0" encoding="utf-8"?>
<MenuItems>
  <MenuItem Text="Menu Item1" ToolTip="" NavigateUrl="?Sel=">
    <SubMenuItem Text="Sub Menu Item1" NavigateUrl="?Sel=">
      <SubSubMenuItem Text="Sub Sub Item1 " NavigateUrl="?Sel=" />
    </SubMenuItem>
  </MenuItem>
  <MenuItem Text="Menu Item2" ToolTip="" NavigateUrl="?Sel=" />
  <MenuItem Text="Menu Item3" ToolTip="" NavigateUrl="?Sel=" />
  <MenuItem Text="Menu Item4" ToolTip="" NavigateUrl="?Sel=" />
  <MenuItem Text="Menu Item5" ToolTip="" NavigateUrl="?Sel=" />
</MenuItems>



Now you need to update your DataItemBinding for Menu control:



Now you need to update your DataItemBinding for Menu control:

<DataBindings>
    <asp:MenuItemBinding DataMember="MenuItem" NavigateUrlField="NavigateUrl" TextField="Text" />
    <asp:MenuItemBinding Depth="1" DataMember="SubMenuItem" NavigateUrlField="NavigateUrl" TextField="Text" />
    <asp:MenuItemBinding Depth="2" DataMember="SubSubMenuItem" NavigateUrlField="NavigateUrl" TextField="Text" />
</DataBindings>



The Depth property extracts member from child node of datasource.

Hope it will help mark as answer if helped.

谢谢,
Hemant



The Depth property extracts member from child node of datasource.

Hope it will help mark as answer if helped.

thanks,
Hemant


这篇关于该文档已经有一个'DocumentElement'节点-Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆