解析xml并连接多个子节点的结果

时间:2013-01-07 17:18:51

标签: c# xml xml-parsing

我需要解析一个xml文件,需要加入节点'driver'的结果。
每个'品牌'节点都是唯一的。

样本xml文件:

<brand name = "ford">   
   <transport category="car">
      <driver name="John, Doe"/>
      <driver name="Jane, Doe"/>
    </transport>
</brand>
<brand name = "opel">
   <transport category="car">
      <driver name="Jerry, Smith"/>
      <driver name="Jeff, Perry"/>
     </transport>
</brand>


预期结果:

John, Doe - Jane, Doe
Jerry, Smith - Jeff, Perry


到目前为止我的代码:

    XmlTextReader reader = new XmlTextReader(@"D:\myfile.xml");

    while (reader.Read())
    {
        if (reader.Name == "brand")
            {
                XmlReader inner =  reader.ReadSubtree();
                while (inner.Read()) ;

                if (reader.Name == "driver")
                {
                    string drvNames = reader.GetAttribute("name");
                }   
            }
    }


我无法获得“驱动程序”的结果。
一个或多个'驱动程序'节点。
语言C#

2 个答案:

答案 0 :(得分:3)

您可以使用Linq to Xml:

var xdoc = XDocument.Load(@"D:\myfile.xml");
var query = from t in xdoc.Descendants("transport")
            select String.Join(" - ", t.Elements("driver")
                                      .Select(e => (string)e.Attribute("name")));

如果无法将整个文件加载到内存中:

XmlTextReader reader = new XmlTextReader(@"D:\myfile.xml");
List<string> result = new List<string>();

while (reader.Read())
{    
    if (reader.Name == "brand")
    {
        XmlReader inner = reader.ReadSubtree();
        List<string> names = new List<string>();

        while (inner.Read())
        {
            if (reader.Name == "driver")
                names.Add(reader.GetAttribute("name"));
        }

        if (names.Any())
            result.Add(String.Join(" - ", names));
    }
}

答案 1 :(得分:1)

using System;
using System.Linq;
using System.Xml.Linq;


class Program
{
    static void Main(string[] args)
    {
        string xml = @"<root>
                        <brand name = 'ford'>   
                           <transport category='car'>
                              <driver name='John, Doe'/>
                              <driver name='Jane, Doe'/>
                            </transport>
                        </brand>
                        <brand name = 'opel'>
                           <transport category='car'>
                              <driver name='Jerry, Smith'/>
                              <driver name='Jeff, Perry'/>
                           </transport>
                        </brand></root>";
        XDocument doc = XDocument.Parse(xml);
        var transports = from t in doc.Root.Elements()
                         select new
                         {
                             Category = t.Attribute("name").Value.Trim(),
                             Drivers = t.Element("transport")
                                        .Elements()
                                        .Select(x => new { Name = x.Attribute("name").Value.Trim() })
                         };
        foreach (var t in transports)
        {
            Console.WriteLine(t.Category);
            foreach (var driver in t.Drivers)
                Console.WriteLine(driver.Name.PadLeft(15));
        }
        Console.ReadKey(true);
    }
}