<?xml version="1.0" encoding="utf-8" ?>
<Menu>
<Navigation text="文件">
<Child text="新建">
<Grandson>项目</Grandson>
<Grandson>网站</Grandson>
<Grandson text="文件">
<Last>文件1</Last>
<Last>文件2</Last>
<Last>文件3</Last>
</Grandson>
</Child>
<Child text="打开">
<Grandson>项目</Grandson>
<Grandson>文件</Grandson>
</Child>
<Child text="添加">
<Grandson>新建项目</Grandson>
<Grandson>新建网站</Grandson>
</Child>
</Navigation>
<Navigation text="编辑">
<Child>剪切</Child>
<Child>复制</Child>
<Child>粘贴</Child>
</Navigation>
<Navigation>视图</Navigation>
<Navigation>网站</Navigation>
</Menu>
这是我的xml文件,我想做成多级导航菜单,我想先通过程序找到第一层菜单,再循环找到是否有子菜单,本人对xml不是很了解,大家帮帮我吧,谢谢,在线等!!!

解决方案 »

  1.   


    XmlDocument xml = new XmlDocument();
    xml.Load("Xml文件地址");XmlNode node = xml.SelectSingleNode("Menu");
    if(node != null)
    {
      foreach(XmlNode n in node.ChildNodes)
      {
        //...............
      }
    }
      

  2.   

    public static string ReadXml(string strKey)//写入动态的数据库配置信息
            {
                string TempValues = "";
                XmlDocument doc = new XmlDocument();
                //获得配置文件的全路径
                string strFileName = Application.StartupPath + "\\assembly.xml";
                doc.Load(strFileName);
                //找出名称为“add”的所有元素
                XmlNodeList nodes = doc.GetElementsByTagName("add");
                for (int i = 0; i < nodes.Count; i++)
                {
                    //获得将当前元素的key属性
                    XmlAttribute att = nodes[i].Attributes["key"];
                    //根据元素的第一个属性来判断当前的元素是不是目标元素
                    if (att.Value == strKey)
                    {
                        att = nodes[i].Attributes["value"];
                        TempValues = att.Value;
                        break;
                    }
                }
                doc.Save(strFileName);
                return TempValues;
            }
      

  3.   

     XmlDocument xmlDoc1 = new XmlDocument();
                xmlDoc1.Load("s.xml");
               XmlNode node = xmlDoc1.SelectNodes(“菜单名字”)node.ChildNodes存放子节点
      

  4.   

    给你一个我用的xMLHelper类:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml;
    using System.Collections.Generic;namespace NetView.Models
    {
       
        /// <summary>
        /// XmlHelper 的摘要说明
        /// </summary>
        public class XmlHelper
        {
            private string msg;
            public XmlHelper()
            {
            }        /// <summary>
            /// 读取数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
            /// <returns>string</returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Read(path, "/Node", "")
             * XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
             ************************************************/
            public static string Read(string path, string node, string attribute)
            {
                string value = "";
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
                }
                catch { }
                return value;
            }            /// <summary>
            /// 插入
            /// </summary>
            /// <param name="path"></param>
            /// <param name="node"></param>
            /// <param name="node1"></param>
            /// <param name="element"></param>
            /// <param name="attribute"></param>
            /// <param name="value"></param>
            public static void Insert(string path, string node, string node1, string element, string attribute, string value, string attribute1, string value1)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node); //查找根node节点
                    XmlElement xel = doc.CreateElement(node1);
                    //if (element.Equals(""))
                    //{
                    //    if (!attribute.Equals(""))
                    //    {
                    //        XmlElement xe = (XmlElement)xe1;
                    //        xe.SetAttribute(attribute, value);
                    //    }
                    //}
                    //else
                    //{
                    XmlElement xe = doc.CreateElement(element);
                    if (attribute.Equals(""))
                        xe.InnerText = value;
                    else
                        xe.SetAttribute(attribute, value);
                    xe.SetAttribute(attribute1, value1);
                    xel.AppendChild(xe);
                    xn.AppendChild(xel);
                    //}
                    doc.Save(path);            }
                catch { }
            }
            /// <summary>
            /// 插入数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
            /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
            /// <param name="value">值</param>
            /// <returns></returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Insert(path, "/Node", "Element", "", "Value")
             * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
             * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
             ************************************************/
            public static void Insert(string path, string node, string element, string attribute, string value)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    if (element.Equals(""))
                    {
                        if (!attribute.Equals(""))
                        {
                            XmlElement xe = (XmlElement)xn;
                            xe.SetAttribute(attribute, value);
                        }
                    }
                    else
                    {
                        XmlElement xe = doc.CreateElement(element);
                        if (attribute.Equals(""))
                            xe.InnerText = value;
                        else
                            xe.SetAttribute(attribute, value);
                        xn.AppendChild(xe);
                    }
                    doc.Save(path);
                }
                catch { }
            }        /// <summary>
            /// 修改数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
            /// <param name="value">值</param>
            /// <returns></returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Insert(path, "/Node", "", "Value")
             * XmlHelper.Insert(path, "/Node", "Attribute", "Value")
             ************************************************/
            public static void Update(string path, string node, string attribute, string value)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    XmlElement xe = (XmlElement)xn;
                    if (attribute.Equals(""))
                        xe.InnerText = value;
                    else
                        xe.SetAttribute(attribute, value);
                    doc.Save(path);
                }
                catch { }
            }        /// <summary>
            /// 删除数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
            /// <param name="value">值</param>
            /// <returns></returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Delete(path, "/Node", "")
             * XmlHelper.Delete(path, "/Node", "Attribute")
             ************************************************/
            public static void Delete(string path, string node, string attribute)
            {
                // XmlDocument doc = new XmlDocument();
                // doc.Load(path);
                // XmlNode xn = doc.SelectSingleNode(node);
                // string mainNode = node.Substring(0, node.LastIndexOf("/"));
                //doc.SelectSingleNode(mainNode).RemoveChild(doc.SelectSingleNode(node));            try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    // XmlNode xn = doc.SelectSingleNode(node);
                    XmlNodeList xnl = doc.SelectSingleNode(node).ChildNodes;
                    foreach (XmlNode xn in xnl)
                    {
                        XmlNodeList xnl1 = xn.SelectSingleNode("RedirectUrl").ChildNodes;
                        XmlElement xe = (XmlElement)xn;
                        for (int j = 0; j < xnl1.Count; j++)
                        {
                            XmlElement xe1 = (XmlElement)xnl1.Item(j);
                            if (xe1.GetAttribute("UrlName") == "小路工作室1231231231")
                            {
                                xn.RemoveChild(xe1);
                                if (j < xnl1.Count) j = j - 1;
                            }
                        }
                        //  if (xe.GetAttribute("genre") == "fantasy")
                        //{
                        //      xe.RemoveAttribute("genre");//删除genre属性
                        //  }
                        //  else if (xe.GetAttribute("genre") == "update李赞红")
                        //{
                        //      xe.RemoveAll();//删除该节点的全部内容
                        //  }
                    }                //for (int i = 0; i < xnl.Count; i++)
                    //{
                    //    XmlNodeList xnl1 = xnl.Item(i).SelectSingleNode("RedirectUrl").ChildNodes;
                    //    for (int j = 0; j < xnl1.Count; j++)
                    //    {
                    //        XmlElement xe = (XmlElement)xnl1.Item(j);
                    //        if (xe.GetAttribute("UrlName") == "小路工作室1231231231")
                    //        {
                    //            xn.RemoveChild(xe);
                    //            if (i < xnl1.Count) i = i - 1;
                    //        }
                    //    }
                    //}                //XmlElement xe = (XmlElement)xn;
                    //if (attribute.Equals(""))
                    //    xn.ParentNode.RemoveChild(xn);
                    //else
                    //    xe.RemoveAttribute(attribute);
                    doc.Save(path);
                }
                catch { }
            }
      

  5.   


            /// <summary>
            /// 删除属性带key的节点,key:GUID
            /// </summary>
            /// <param name="path">XML文件路径</param>
            /// <param name="rootnode">根节点</param>
            /// <param name="key">key:Guid</param>
            /// <param name="msg">返回的信息</param>
            public static void DeleteXMLNode(string path, string rootnode, string znode,string type,string key)
            {
              try
                {
                if (System.IO.File.Exists(path))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(path);
                    XmlNodeList xl = xmlDoc.SelectSingleNode(rootnode).ChildNodes;
                    for (int i = 0; i < xl.Count; i++)
                    {
                        XmlElement xe = (XmlElement)xl[i];//第i个dbGust子节点
                        XmlNodeList node = xe.GetElementsByTagName(znode);
                        if (node.Count > 0)
                        {
                            for (int j = 0; j < node.Count; j++)
                            {
                                XmlNode node1 = node.Item(j);
                                XmlElement xe1 = (XmlElement)node.Item(j);
                                if (xe1.GetAttribute(type) == key)
                                {
                                    xmlDoc.SelectSingleNode(rootnode).RemoveChild(node[0].ParentNode);//删除该节点
                                    break;
                                }
                            }
                           
                        }
                    }
                    
                        xmlDoc.Save(path);
                    }
              }
                    catch (XmlException ex)
                    {                }            }
                /// <summary>
                /// 返回符合指定名称的节点数
                /// </summary>
                /// <param name="nodeName">节点名</param>
                /// <returns>节点数</returns>
                public static int Count(string path,string nodeName,string znode)
                {
                    try
                    {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(path);
                        XmlElement xe = (XmlElement)xmlDoc.SelectSingleNode(nodeName); //查找根node节点
                        XmlNodeList nodeList = xe.GetElementsByTagName(znode);
                        return nodeList.Count;
                    }
                    catch (Exception e)
                    {
                        throw (new Exception(e.Message));
                    }
                }
            /// <summary>
            /// 返回最后的数字
            /// </summary>
            /// <param name="path"></param>
            /// <param name="nodeName"></param>
            /// <param name="znode"></param>
            /// <returns></returns>
            public static string num(string path, string nodeName, string znode,string type)
            {
                try
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(path);
                    XmlElement xe = (XmlElement)xmlDoc.SelectSingleNode(nodeName); //查找根node节点
                    XmlNodeList nodeList = xe.GetElementsByTagName(znode);
                    int i = nodeList.Count;
                    XmlElement xe1 = (XmlElement)nodeList.Item(nodeList.Count-1);
                    string inum=xe1.GetAttribute(type);
                     
                    return inum;
                }
                catch (Exception e)
                {
                    throw (new Exception(e.Message));
                }
            }
            }
          
        }
      

  6.   

    <?xml version="1.0" encoding="utf-8" ?>
    <Menu>
    <Navigation text="文件">
    <Child text="新建">
    <Grandson text="项目"></Grandson>
    <Grandson text="网站"></Grandson>
    <Grandson text="文件">
    <Last text="文件1"></Last>
    <Last text="文件2"></Last>
    <Last text="文件3"></Last>
    </Grandson>
    </Child>
    <Child text="打开">
    <Grandson text="项目"></Grandson>
    <Grandson text="文件"></Grandson>
    </Child>
    <Child text="添加">
    <Grandson text="新建项目"></Grandson>
    <Grandson text="新建网站"></Grandson>
    </Child>
    </Navigation>
    <Navigation text="编辑">
    <Child text="剪切"></Child>
    <Child text="复制"></Child>
    <Child text="粘贴"></Child>
    </Navigation>
    <Navigation text="视图"></Navigation>
    <Navigation text="网站"></Navigation>
    </Menu>
    if (!IsPostBack)
            {
                XmlDocument xmldoc = new XmlDocument();            xmldoc.Load(Server.MapPath("TestXMLFile.xml"));
                XmlNode worknode;
                MenuItem rootnode;
                for (int i = 0; i < xmldoc.DocumentElement.ChildNodes.Count; i++)
                {
                    worknode = xmldoc.DocumentElement.ChildNodes[i];
                    if (worknode.ChildNodes.Count > 0)
                        rootnode = BindXmlToMenu(worknode);
                    else
                        rootnode = new MenuItem(worknode.Attributes["text"].Value);
                    this.Menu3.Items.Add(rootnode);
                }
    }MenuItem BindXmlToMenu(XmlNode rootnode)
        {
            XmlNode worknode = rootnode;//当前遍历的节点          MenuItem retnode = new MenuItem(worknode.Attributes["text"].Value); //要返回的菜单节点 
            MenuItem newch;//中间菜单节点          if (worknode.ChildNodes.Count < 1)
            {//没有子节点,生成一个菜单节点返回 
                newch = new MenuItem(worknode.Attributes["text"].Value);
                retnode = newch;
            }
            else //有子节点,调用递归 
            {
                for (int i = 0; i < worknode.ChildNodes.Count; i++)
                {
                    retnode.ChildItems.Add(BindXmlToMenu(worknode.ChildNodes[i]));
                }
            }
            return retnode; 
        }
    谢谢大家,这是我后面改的,现在贴出来大家一起参考,有更好的方法的都可以来讨论
      

  7.   

    XmlDocument xml = new XmlDocument();
    xml.Load("Xml文件地址");XmlNode node = xml.SelectSingleNode("Menu");
      foreach(XmlNode n in node.ChildNodes)
      {
        XmlNode  nodes = n.nodes.add(n.Tex);
       //如果要继续遍历,再循环 
    }
      

  8.   

    你这样安排xml节点层次不好,如果还有更低级的子菜单,是不是就要增加更多的节点名? 下面这样好些:
    <MenuItem text="文件">
      <MenuItem text="新建">
        <MenuItem>项目</Grandson>
        <MenuItem>网站</Grandson>
        <MenuItem text="文件">
          <MenuItem>文件1</MenuItem>
          <MenuItem>文件2</MenuItem>
        </MenuItem>
      </MenuItem>
      <MenuItem text="打开">
        <MenuItem>项目</MenuItem>
      </MenuItem>
    </MenuItem>
      

  9.   

    楼上写的好啊~~很详细[align=center]***********************************************************
                       欢迎使用 CSDN 小秘书
                  CSDN 小秘书下载
    ***********************************************************[/align]
      

  10.   

    谢谢jjcheung,这个问题我也发现了,确实这样好的多
      

  11.   

    顺便在这里再请教大家一个问题,比如,在网页中,我有一些lable控件,还有一个gridview,现在我想把这个网页里的这些导出到word,这怎么做呢?有没有哪位大侠做过,指点下吧!!谢谢
      

  12.   

    导出到word,可以用Microsoft.Office.Interop.Word里面的东西,它是利用COM来调用word的功能,只要是装了office的机器,就可以添加这个程序集的引用来用. 网上很多示例代码的