zoukankan      html  css  js  c++  java
  • C# 读取xml中的配置信息,并加入到combobox或者其他中(winform)

    <?xml version="1.0" encoding="utf-8" ?>
    <webSiteList>
    <site>
     
    <siteName>天涯社区</siteName>
     
    <itemlist>http://www.tianya.cn</itemlist>
     
    <itemlist>综合的论坛讨论社区</itemlist>
     
    </site>
    <site>
     
    <siteName>猫扑网</siteName>
     
    <itemlist>http://www.mop.com</itemlist>
     
    <itemlist>国内最大的分栏式论坛,综合社区</itemlist>
     
    <itemlist>没啥好说的了!</itemlist>
     
    </site>
    </websitelist>

    设combobox里的是猫扑网 天涯社区 所在的siteName
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                XmlDocument doc = new XmlDocument();

                doc.Load("c:\\aa.xml");
                XmlNode node = doc.SelectSingleNode("webSiteList/site[siteName='" + this.comboBox1.Text + "']");
                for (int i = 0; i < node.ChildNodes.Count; i++)
                {
                    Console.WriteLine(node.ChildNodes[i].InnerText);
                }
            }


    XmlDocument doc = new XmlDocument();

                doc.Load(@"D:\Project\C#Test\WindowsApplication1\WindowsApplication2\XMLFile3.xml");

                XmlNode node = doc.SelectSingleNode("/webSiteList/site[siteName='[color=#FF0000]天涯社区'[/color]]");//combox.Text
                foreach (XmlNode el in node.ChildNodes)
                {
             
                        Debug.WriteLine("nodeType:" + el.NodeType);
                        Debug.WriteLine("Text:" + el.InnerText);
                }


    以下内容为程序代码:

    1 - <items num="6">
    2 <item>album</item>
    3 <item>calender</item>
    4 <item>card</item>
    5 <item>notebook</item>
    6 <item>personulbook</item>
    7 <item>poster</item>
    8 </items>

    以上为xml代码

    以下为C#代码

                XmlDocument doc = new XmlDocument();


                doc.Load(Application.StartupPath + "\\xmldocument.xml");  //读取的xml文件路径


                XmlNodeList node = doc.GetElementsByTagName("item");   


                for (int i = 0; i < node.Count; i++)
                {
                    comboBox.Items.Add(node[i].InnerText);
                }
                comboBox.SelectedIndex = 0;


    msdn资料
    http://msdn.microsoft.com/zh-cn/library/system.xml.xmldocument_members(VS.80).aspx    XmlDocument 成员
    http://msdn.microsoft.com/zh-cn/library/system.xml.xmlnode_members(VS.80).aspx           XmlNode 成员
    http://msdn.microsoft.com/zh-cn/library/ms256199.aspx                                                   XPath 表达式的上下文  
  • 相关阅读:
    Makefile编写基础知识总结
    Linux开发基础篇开发环境搭建
    较常用的Linux 命令技巧
    Linux socket实现非阻塞型通信
    VirtualBox linux 挂载共享Windows文件夹
    Struts2 注解基础
    DB2 Error Code: 1218, SQL State: 57011
    半小时内使用vim的常用命令,以及平时使用的感慨
    vim的一些配置。
    link的属性media的用处
  • 原文地址:https://www.cnblogs.com/bnuvincent/p/1550043.html
Copyright © 2011-2022 走看看