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 表达式的上下文  
  • 相关阅读:
    【android】 判断文件是否存在,ImageView scaletype
    【live】回老家,那些感触
    【android】 浏览文件,如浏览sd卡下的图片文件
    【android】java.lang.NoClassDefFoundError或classnotfount等异常错误
    【android】style和theme
    【android】两个按钮的宽度各占屏幕的一半
    【java】html解析
    关于敏感词过滤的一点想法
    JAVA中Vector与ArrayList异同
    MySQL实用语句 GROUP BY ... HAVING ...
  • 原文地址:https://www.cnblogs.com/bnuvincent/p/1550043.html
Copyright © 2011-2022 走看看