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" ]));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:XmlDataSource ID =" XmlDataSource" runat =" server" TransformFile =" ~/TransformXSLT.xsl" XPath =" MenuItems/MenuItem" >
plz help
Please, help. 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 =" " / >
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 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 >
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.
<? 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:
<? 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:
< 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.
thanks,
Hemant <CategoryIDToSub>1</CategoryIDToSub>
<subcategoryid>1</subcategoryid>
<SubCategoryName>Sub Menu Item1</SubCategoryName>
<SubSubMenu>
<SubCategoryID2>1</SubCategoryID2>
<Sub2CategoryName>Sub Sub Item1 </Sub2CategoryName>
</SubSubMenu>
</SubMenu>
</Menu>
<CategoryID>2</CategoryID>
<CategoryName>Menu Item2</CategoryName>
</Menu>
<CategoryID>3</CategoryID>
<CategoryName>Menu Item3</CategoryName>
</Menu>
<CategoryID>4</CategoryID>
<CategoryName>Menu Item4</CategoryName>
</Menu>
<CategoryID>5</CategoryID>
<CategoryName>Menu Item5</CategoryName>
</Menu>
</NewDataSet>
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •