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 表达式的上下文  
原文地址:https://www.cnblogs.com/bnuvincent/p/1550043.html