在尝试将数据集从数据集表转换为xml以使XmlDataSource将其提供给动态菜单控件时,我得到了此异常。
这是背后的代码
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文件代码
<?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代码
<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>
答案 0 :(得分:4)
您的XML文档格式不正确。您有多个文档(根)元素。例如:
<a>some</a>
<a>thing</a>
不格式良好。鉴于:
<list>
<a>some</a>
<a>thing</a>
</list>
状态良好。