精华内容
下载资源
问答
  • c# winform读取xml创建菜单

    千次阅读 2013-09-13 15:27:41
    c# winform读取xml动态创建菜单

      动态创建菜单使得程序灵活性大大增加,本文根据读取xml文件中的配置菜单项来动态创建菜单,代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using WinformMenu.Helper;
    using System.Xml;

    namespace WinformMenu
    {
        public partial class FrmAddMenu : Form
        {
            public FrmAddMenu()
            {

                InitializeComponent();

            }

            private void FrmAddMenu_Load(object sender, EventArgs e)
            {
                MenuStrip menuS = new MenuStrip();
                menuS.Parent = this;
                string xmlFileName = @"F:\c#工程\WinformMenu\WinformMenu\XML\菜单.xml";
                createMenu(xmlFileName, menuS);
            }
           
           
            public void createMenu(string xmlFileName, MenuStrip menuS)
            {  
                //1:根据XmlElement创建菜单
                //XmlNode node = xmlHelper.GetXmlNode(xmlFileName, "//MangeTvMenu");
                //foreach (XmlNode xml in node.ChildNodes)
                //{
                //    ToolStripMenuItem mainMenu=new ToolStripMenuItem();
                //    mainMenu.Text = xml.Attributes["descript"].Value;
                //    menuS.Items.Add(mainMenu);
                //    if (xml.HasChildNodes)
                //    {
                //        CreateSubMenuByNode(xml, mainMenu);
                //    }
                //}
                //2:根据XmlNode创建菜单
                XmlElement xml = xmlHelper.GetXmlElement(xmlFileName);
                foreach (XmlElement xel in xml.ChildNodes)
                {
                    ToolStripMenuItem mainMenu = new ToolStripMenuItem();
                    mainMenu.Text = xel.Attributes["descript"].Value;
                    menuS.Items.Add(mainMenu);
                    if (xml.HasChildNodes)
                    {   
                        CreateSubMenuByElement(xel, mainMenu);
                    }
                }
               
            }

            /// <summary>
            /// 根据node节点递归创建菜单
            /// </summary>
            /// <param name="xNode"></param>
            /// <param name="menuItem"></param>
            public void CreateSubMenuByNode(XmlNode xNode, ToolStripMenuItem menuItem)
            {
                foreach (XmlNode xml in xNode.ChildNodes)
                {
                    ToolStripMenuItem subMenu = new ToolStripMenuItem();
                    if (xml.ChildNodes.Count > 1)
                    {
                        subMenu.Text = xml.Attributes["descript"].Value;
                        menuItem.DropDown.Items.Add(subMenu);
                        CreateSubMenuByNode(xml, subMenu);
                    }
                    else
                    {
                        subMenu.Text = xml.InnerText;
                        menuItem.DropDown.Items.Add(subMenu);
                    }
                }
            }

            /// <summary>
            /// 根据元素节点递归创建菜单
            /// </summary>
            /// <param name="xml"></param>
            /// <param name="menuItem"></param>
            public void CreateSubMenuByElement(XmlElement xml, ToolStripMenuItem menuItem)
            {
                foreach (XmlElement xel in xml.ChildNodes)
                {
                    ToolStripMenuItem subMenu = new ToolStripMenuItem();
                    if (xel.ChildNodes.Count>1)
                    {
                        subMenu.Text = xel.Attributes["descript"].Value;
                        menuItem.DropDown.Items.Add(subMenu);
                        CreateSubMenuByElement(xel, subMenu);
                    }
                    else
                    {
                        subMenu.Text = xel.InnerText;
                        menuItem.DropDown.Items.Add(subMenu);
                    }
                }
            }
     ///
        }
    }

    xml文件格式如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <MangeTvMenu descript="管理">
      <Add descript="添加">
        <Employee>员工</Employee>
        <Deptment>部门</Deptment>
      </Add>
      <EmployeeInfo descript="员工信息">
        <BaseInfo>个人基本信息</BaseInfo>
        <StudyInfo>学历信息</StudyInfo>
        <MarryInfo>婚姻信息</MarryInfo>
      </EmployeeInfo>
      <DeptmentInfo descript="部门信息">
        <dept1>部门1</dept1>
        <dept2>部门2</dept2>
      </DeptmentInfo>
      <MoneyManage descript="工资管理">
        <BaseMoney>基本工资</BaseMoney>
        <OtherMoney>绩效工资</OtherMoney>
      </MoneyManage>
      <Apply descript="申请">
        <AddWork>加班</AddWork>
        <ApplyWeek>请假</ApplyWeek>
        <AddMoney>加薪</AddMoney>
        <Another>其他</Another>
      </Apply>
    </MangeTvMenu>

    效果如下图所示:

     

    xmlHelper为自己写的一个操作xml的类,由于比较简单在此就不贴代码了。

    发现如果将xel.ChildNodes.Count>1(也就是上面红色字体代码)换成xel.HasChildNodes程序就会报错,目前正在思考此问题,欢迎广大朋友指点。

    展开全文
  • 动态创建菜单使得程序灵活性大大增加,本文根据读取xml文件中的配置菜单项来动态创建菜单,代码如下
  • 在调用方法之前,要右击选中的xml文件,设置xml 的 Coyp to output direcotory :Copy always  XML: &lt;?xml version="1.0" encoding="utf-8" ?&gt;&lt;!--用户个性皮肤--&...

    在调用方法之前,要右击选中的xml文件,设置xml 的 Coyp to output direcotory :Copy always

     XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <!--用户个性皮肤-->
    <Skin>
      <DefaultSkin>blue</DefaultSkin>
    </Skin>

    winform方法:

     /// <summary>
            /// 读取默认皮肤
            /// </summary>
            /// <returns></returns>
            public string ReadDefaultSkin()
            {
                //Server.MapPath(@"Projects.xml");

                XmlReader reader = new XmlTextReader(Application.StartupPath + @"\Xml\SkinConfig.xml");
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
                XmlNode root = doc.DocumentElement;
                //选取DefaultLangugae节点
                XmlNode node = root.SelectSingleNode("//DefaultSkin");
                string result = "blue";
                if (node != null)
                {
                    //取出节点中的内容
                    result = node.InnerText;
                }
                reader.Close();
                return result;
            }


            /// <summary>
            /// 更新皮肤
            /// </summary>
            /// <param name="FileName"></param>
            /// <param name="skin"></param>
            public void UpdateSkin(string skin)
            {
                string FileName = Application.StartupPath + @"\Xml\SkinConfig.xml";
                //初始化XML文档操作类
                XmlDocument myDoc = new XmlDocument();
                //加载XML文件
                myDoc.Load(FileName);

                //搜索指定的节点
                System.Xml.XmlNodeList nodes = myDoc.SelectNodes("//Skin");

                if (nodes != null)
                {
                    foreach (System.Xml.XmlNode xn in nodes)
                    {
                        xn.SelectSingleNode("DefaultSkin").InnerText = skin;
                    }
                }

                myDoc.Save(FileName);

            }

     

    展开全文
  • c#winform操作xml文件

    2017-07-31 08:56:50
    <?xml version="1.0" encoding="utf-8"?> <config name="Contidion1">127.1.1.111 <config name="Contidion2">127.0.0.2 ``` 请问用database怎么能保存成这种格式的xml文件呢?
  • webform读取: string.Format(SqlConfig.GetSql("SQLServer.QueryTableInfo"), "SysLog"); ///  /// 读取指定SQL  ///  /// sql名称  /// sql字符串  public static string GetSql

    webform读取:

    string.Format(SqlConfig.GetSql("SQLServer.QueryTableInfo"), "SysLog");

    /// <summary>
            /// 读取指定SQL
            /// </summary>
            /// <param name="key">sql名称</param>
            /// <returns>sql字符串</returns>
            public static string GetSql(string key)
            {
                string sql = string.Empty;
                try
                {
                    string[] sqlLayers = key.Split('.');
                    XmlNode xmlNode = ReadMapping(sqlLayers[0]);
                    sql = xmlNode.SelectNodes("SqlMaps[@key='" + sqlLayers[1] + "']/SqlMap[@key='" + sqlLayers[2] + "']")[0].InnerText;
                }
                catch (Exception ex)
                {
                    DataLib.LogAccess.Log("获取SQL:" + key + "错误:" + ex.ToString());
                }
                return sql;
            }

            /// <summary>
            /// 读取配置文件中的Mapping节点
            /// </summary>
            /// <param name="key">mapping节点名称</param>
            /// <returns>Mapping节点下的sqlMap</returns>
            private static XmlNode ReadMapping(string key)
            {
                XmlDocument Xdoc;
                string fileName = HttpContext.Current.Server.MapPath("~/mapping.config");
                //if (HttpContext.Current.Cache["mapping"] == null)
                //{
                    Xdoc = new XmlDocument();
                    Xdoc.Load(fileName);
                    HttpContext.Current.Cache.Add("mapping", Xdoc, new System.Web.Caching.CacheDependency(fileName),
                        DateTime.Now.AddDays(3), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
                //}
                //else
                    Xdoc = (XmlDocument)HttpContext.Current.Cache["mapping"];
                return Xdoc.SelectSingleNode("/configuration/mapping[@key='" + key + "']");
            }
        }

     

    winfrom读取

    string.Format(SqlConfig.GetSql("SQLServer.QueryTableInfo"), "SysLog");

     /// <summary>
            /// 读取指定SQL
            /// </summary>
            /// <param name="key">sql名称</param>
            /// <returns>sql字符串</returns>
            public static string GetSql(string key)
            {
                string sql = string.Empty;
                try
                {
                    string[] sqlLayers = key.Split('.');
                    XmlNode xmlNode = ReadMapping(sqlLayers[0]);
                    XmlNodeList list = xmlNode.SelectNodes("Sql");
                    foreach (XmlNode node in list)
                    {
                        if (node.Attributes["key"].Value.Equals(sqlLayers[1]))
                        {
                            return node.InnerText;
                        }
                    }
                    sql = xmlNode.SelectNodes("SqlMap[@key='" + sqlLayers[1] + "']")[0].InnerText;
                }
                catch (Exception ex)
                {
                    DataLib.LogAccess.Log("获取SQL:" + key + "错误:" + ex.ToString());
                }
                return sql;
            }

            /// <summary>
            /// 读取配置文件中的Mapping节点
            /// </summary>
            /// <param name="key">mapping节点名称</param>
            /// <returns>Mapping节点下的sqlMap</returns>
            private static XmlNode ReadMapping(string key)
            {
                XmlDocument Xdoc;
                string fileName = System.Windows.Forms.Application.StartupPath + (@"\SqlMapping.config");
                Xdoc = new XmlDocument();
                Xdoc.Load(fileName);
                XmlNodeList list = Xdoc.SelectNodes("SqlMapping/SqlMaps");
                foreach (XmlNode node in list)
                {
                    if (node.Attributes["key"].Value.Equals(key))
                    {
                        return node;
                    }
                }
                return null;
            }
        }

    <?xml version="1.0" encoding="utf-8" ?>
    <SqlMapping>
      <SqlMaps key="SQLServer">
        <Sql key="QueryAllTable">
          <![CDATA[
            select name from sysobjects where type='U' and name <>'sysdiagrams' order by name asc
          ]]>
        </Sql>

      </SqlMaps>

    </SqlMapping>

     

    展开全文
  • 源码采用三层架构,实现了C#对XML文件的增、删、改、查。简单易懂,包括详细的代码注释。
  • 注释很详细 在网上看了看,对于新手来说很困难 所以我这个新手就写了写 测试通过 原创!
  • XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"D:\abc.xml"); XmlElement xmlRoot = xmlDoc.DocumentElement; foreach (XmlNode node in xmlRoot.ChildNodes) { label1.Text = node["Label"]....
  • winformxml文件的操作

    2012-11-26 14:19:56
    winformxml文件的操作
  • C# winform 利用XmlDocument 读取xml文档的多种方法。 using System; using System.Xml; using System.Xml.XPath; using System.Data; class ReadXML { public st...

    C# winform 利用 XmlDocument 读取xml文档的多种方法。

       using    System;   
       using    System.Xml;   
       using    System.Xml.XPath;   
       using    System.Data;   
        
       class    ReadXML   
       {   
           public    static    void    Main()   
           {   
       string    sFile    =    "ReadXml.xml";   
        
       //method    1   
       XmlDocument    doc    =    new    XmlDocument();   
       doc.Load(sFile);   
       XmlNode    node    =    doc.DocumentElement["News"]["Content"];   
       Console.WriteLine(node.InnerText);   
        
       //method2   
       node    =    doc.SelectSingleNode("//Content");   
       Console.WriteLine(node.InnerText);   
        
       //similarly   
       node    =    doc.DocumentElement.SelectSingleNode("News/Content");   
       Console.WriteLine(node.InnerText);   
        
       //method    3   
       DataSet    ds    =    new    DataSet();   
       ds.ReadXml(sFile);   
       Console.WriteLine(ds.Tables[0].Rows[0]["Content"].ToString());   
        
       //method    4   
       XmlTextReader    reader    =    new    XmlTextReader(sFile);   
       while    (reader.Read())   
       {   
       if    (reader.Name    ==    "Content")   
       {   
       Console.WriteLine("***"    +    reader.ReadString());   
       break;   
       }   
       }   
        
       reader.Close();   
        
       //method    5   
        
       XPathDocument    xpdoc    =    new    XPathDocument(sFile);   
       XPathNavigator    xpnv    =    xpdoc.CreateNavigator();   
       xpnv.MoveToFirstChild();   
       xpnv.MoveToFirstChild();   
       xpnv.MoveToFirstChild();   
       xpnv.MoveToNext();xpnv.MoveToNext();xpnv.MoveToNext();   
       Console.WriteLine("pathnavigator:"    +    xpnv.Value);   
           }   
       }

    via

    转载于:https://www.cnblogs.com/flymoo/archive/2011/06/23/2088349.html

    展开全文
  • winformxml的基本操作

    2014-08-14 16:40:32
    xmlwinform下的操作,自己写测试正常,共享
  • C# winform 使用XML文件

    千次阅读 2019-04-10 10:16:43
    C# Winform 简易XML文件存取 最近开始学C#视窗,记录一下关于最近学习的方法,参考链接如下。 http://www.weisico.com/program/2016/0903/377.html 我用的环境是Visual Studio 2015,最近再练习TCP/IP和COM串口的...
  • foreach (XmlNode node in list) { if (node.Name == "Soft" && node.Attributes["Name"].Value.ToLower() == "Version") { foreach (XmlNode xml in node) { if (xml.Name == "Verson") newVerson = xml.InnerText...
  • C# winform xml文件读取

    2017-11-27 15:20:42
    判断xml文件是否存在;创建xml文件;遍历读取xml文件节点;添加节点等功能
  • winform 使用XML读写类型源码! 很值得下载看看!资源免费,大家分享!!
  • 一、利用读取xml获取web.config中的数据库连接 参考:传送门 #region 利用读取xml获取webconfig private static String GetXML() { //待修改 String f = System.Windows.Forms.Application.StartupP...
  • .net vs2010开发 winform导入xml利用treeview控件对其增删改查源码 已全部实现功能 同步操作xml
  • Winform读取XML节点

    2017-09-13 08:30:00
    XmlDocument xmldoc = new XmlDocument(); xmldoc.Load("PrintConfig.xml"); XmlNode node = xmldoc.SelectSingleNode("//Config[name='C1']"); string TempX = string.Empty; string TempY = string.Empty; ...
  • C# WinForm 读写XML文件

    2019-10-04 12:46:17
    建立一个WinForm应用程序 添加MenuStrip控件,填写两个功能“读取” 和“导出数据”。 用了两个DataSet控件和对话框“打开(OpenFilesDialog控件)"和"保存(SaveFilesDialog控件)" 读取 privatevoid读取...
  • C#winform xml操作详解

    2015-08-04 08:56:53
    详细的xml操作,包教包会 有对xml的增删改查
  • WinForm 读写XML文件

    2008-07-26 22:24:00
    建立一个WinForm应用程序 添加MenuStrip控件,填写两个功能“读取” 和“导出数据”。用了两个DataSet控件和对话框“打开(OpenFilesDialog控件)"和"保存(SaveFilesDialog控件)"读取 private void 读取...
  • WinFormXML中动态加载菜单的示例

    千次阅读 2012-08-06 09:03:46
    WinFormXML中动态加载菜单的示例 这个案例是一个基于VS2005的WinForm菜单控件的例子,功能是从XML中动态加载菜单数据到MenuTrip控件上。这个案例中用到了XML,反射之类的技术。当然实用价值有限,仅供学习参考...
  • 读入到DataSet,用方法:ds.ReadXml("datasetXml.xml"); 绑定到Grid中,this.dataGrid1.SetDataBinding(ds,"COLUMN");其中COLUMN为xml中的表名。 其中xml文档的代码为: <?xml version="1.0" encoding="utf-8...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 11,431
精华内容 4,572
关键字:

winform读取xml