将xml多个相同下节点的分类获取 C#

2025-01-04 06:28:50
推荐回答(2个)
回答1:

//数据都帮你取出来完了,摆控件赋值自个弄弄就好了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace XMLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load("aa.xml");
            XmlNode xe = xml.SelectSingleNode("Information/SoftWare/Browsers");
            foreach (XmlNode item in xe.ChildNodes)
            {
                if (item.Name == "Browser")
                {
                    if (item.SelectSingleNode("Name").InnerText == "Mozilla Firefox")
                    {
                        Console.WriteLine(item.SelectSingleNode("Name").InnerText);
                        Console.WriteLine(item.SelectSingleNode("Version").InnerText);
                        Console.WriteLine(item.SelectSingleNode("HomePage").InnerText);
                        XmlNode searchEnginesNode = item.SelectSingleNode("SearchEngines");
                        foreach (XmlNode itemEngine in searchEnginesNode.ChildNodes)
                        {
                            Console.WriteLine(itemEngine.SelectSingleNode("Name").InnerText);
                        }
                    }
                    else if (item.SelectSingleNode("Name").InnerText == "Google Chrome")
                    {
                        Console.WriteLine(item.SelectSingleNode("Name").InnerText);
                        Console.WriteLine(item.SelectSingleNode("Version").InnerText);
                        Console.WriteLine(item.SelectSingleNode("HomePage").InnerText);
                        XmlNode searchEnginesNode = item.SelectSingleNode("SearchEngines");
                        foreach (XmlNode itemEngine in searchEnginesNode.ChildNodes)
                        {
                            Console.WriteLine(itemEngine.SelectSingleNode("Name").InnerText);
                        }
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

回答2:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("~/Config/BusinessCategory.xml"));
XmlElement rootElem = xmlDoc.DocumentElement; //获取根节点
XmlNodeList BusinessCategoryNodes = rootElem.GetElementsByTagName("BusinessCategory");
string html = "";
List _lBusinessTypeOptions = new List();
_lBusinessTypeOptions = obj_BusinessTypeOptions.GetByCategoryID(CategoryID);
foreach (XmlNode node in BusinessCategoryNodes)
{
}
读取XML