Dear all;

I have the following code below and I have been spending time trying to debug it but I can't understand why I am getting the illegal character error message. See my code below. Thank you.

workheader.master.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Xml;

public partial class workheader : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        load();
    }

    protected void load()
    {
        
       
       XDocument doc = XDocument.Load(Server.MapPath("App_Data/XMLFile.xml"));

        foreach (XElement menuItem in doc.Elements("MainMenu").Elements("MenuItem"))
        {
            string a = menuItem.Element("LinkURL").Value;
            string b = menuItem.Element("LinkLabel").Value;


            hp.Text = b;
            hp.NavigateUrl = a;

            if (doc.Elements("submenu") != null)
            {
                foreach (XElement subItem in doc.Elements("MainMenu").Elements("MenuItem").Elements("SubMenu").Elements("SubMenuItem"))
                {
                    string c = subItem.Element("LinkURL").Value;
                }
            }

        }


    }

}

workheader.master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="workheader.master.cs" Inherits="workheader" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">

    <asp:HyperLink ID="hp" runat="server" />

    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
        
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

XML code

<?xml version="1.0" encoding="utf-8" ?>
<MainMenu>
	<MenuItem>
		<LinkLabel>Begin</LinkLabel>
		<LinkURL>DefaultA.aspx</LinkURL>
	</MenuItem>
	<MenuItem>
		<LinkLabel>
			Default B
		</LinkLabel>
		<LinkURL>
			DefaultB.aspx
		</LinkURL>
	</MenuItem>

	<MenuItem>
		<LinkLabel>
			Default reports
		</LinkLabel>
		<LinkURL>
			DefaultC.aspx
		</LinkURL>
		<SubMenu>
			<SubMenuItem>
				<LinkLabel>
					Default C established
				</LinkLabel>
				<LinkURL>
					DefaultC.aspx
				</LinkURL>
			</SubMenuItem>

			<SubMenuItem>
				<LinkLabel>
					Default D
				</LinkLabel>
				<LinkURL>
					DefaultD.aspx
				</LinkURL>
			</SubMenuItem>

			<SubMenuItem>
				<LinkLabel>
					Default E
				</LinkLabel>
				<LinkURL>
					DefaultE.aspx
				</LinkURL>
			</SubMenuItem>
			
			
		</SubMenu>
	</MenuItem>
</MainMenu>

All help is appreciated. Thank you.

Xml preserve the whitespaces so you need to use Trim() to remove blanks from the both side of the string value.

hp.Text = b.Trim();
 hp.NavigateUrl = a.Trim();

Ok, that fix the illegal error problem, however though the code is currently not doing much unfortunately, it doesnt show the associated submenu when the main menu is clicked on unfortunately.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.