我试着从我创建的字符串中读取一些XML,但实际上任何XML文件都可以。
我只想像浏览多维矩阵一样浏览XML节点,并最终将它们放在数据表中(将它们放在带有sqlbulkcopy的SQL服务器中)。我已经查过MSDN和附近的资料了。有人能简单明了地解释一下吗?
这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Xml;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        private static DataTable table = new DataTable();
        private static String xmlString =
        @"<?xml version='1.0'?>
        <!-- This is a sample XML document -->
        <Garage>
            <Car>
                <Name>Ferrari</Name>
                <Speed>360km/h</Speed>
                <Engine>Ferrari Enzo</Engine>
                <Color>Red</Color>
                <Year>1999</Year>
            </Car>
            <Car>
                <Name>Maserati</Name>
                <Speed>270km/h</Speed>
                <Color>Metal Grey</Color>
                <Year>2007</Year>
            </Car>
            <Car>
                <Name>Limo</Name>
                <Color>Black</Color>
                <Engine>Chevrolet</Engine>
                <Year>2007</Year>
            </Car>
        </Garage>";

        static void Main(string[] args)
        {
            Program x = new Program();
            XmlReader reader = XmlReader.Create(new StringReader(xmlString));
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    Console.WriteLine(XmlNodeType.Element.ToString());
                    }
                }
            }

        }
    }

我想把整件事循环一下,然后得到如下信息:
姓名:法拉利
速度:360km/h
发动机:法拉利恩佐
等等,你得到了训练。

最佳答案

System.Xml.Linq.XDocument.Parse(string)会给你一个XDocumentXDocument.Root将获得文档的根目录。我想你会发现这很容易处理。
您正在尝试在.NET中使用上一代XML工具-较新的XElementXDocument等工具更容易访问。
样例代码

using System.Xml.Linq;
...
var root = XDocument.Parse(xmlString).Root;

var cars = root
    .ToAll("Car")
    .Select(car => new
    {
        Name = car.ToFirst("Name").Value,
        Speed = car.ToAll("Speed").Any() ? car.ToFirst("Speed").Value : null,
        Color = car.ToFirst("Color").Value,
        Engine = car.ToFirst("Engine").Value,
        Year = int.Parse(car.ToFirst("Year").Value)
    })
    .ToList();

辅助类
public static class XmlHelper
{
    public static XNode ReadFrom(Stream stream)
    {
        using (var xmlReader = XmlReader.Create(stream))
            return XDocument.Load(xmlReader);
    }

    public static void WriteTo(Stream stream, XNode node)
    {
        using (var xmlWriter = XmlWriter.Create(stream))
            node.WriteTo(xmlWriter);
    }

    public static XElement ToFirst(this XElement ancestor, String descendantLocalName)
    {
        return ancestor.Descendants().FirstOrDefault(element => element.Name.LocalName == descendantLocalName);
    }

    public static IEnumerable<XElement> ToAll(this XElement ancestor, String descendantLocalName)
    {
        return ancestor.Descendants().Where(element => element.Name.LocalName == descendantLocalName);
    }

    public static string ToAttribute(this XElement element, string name)
    {
        var attribute = element.Attribute(XName.Get(name));
        return attribute != null ? attribute.Value : null;
    }
}

10-07 20:03
查看更多