的XDocument或XmlDocument的 [英] XDocument or XmlDocument

查看:134
本文介绍了的XDocument或XmlDocument的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习<一href=\"http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx\"><$c$c>XmlDocument但我刚刚跑进<一个href=\"http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx\"><$c$c>XDocument当我尝试搜索的区别或他们的利益我找不到有用的东西,你能告诉我为什么你会使用一个比另一个?

I am now learning XmlDocument but I've just ran into XDocument and when I try to search the difference or benefits of them I can't find something useful, could you please tell me why you would use one over another ?

推荐答案

如果您正在使用.NET版本3.0或更低,你的有无的使用的XmlDocument 又名经典的DOM API。同样,你会发现有哪个会想到这一些其他的API。

If you're using .NET version 3.0 or lower, you have to use XmlDocument aka the classic DOM API. Likewise you'll find there are some other APIs which will expect this.

如果你的选择,但是,我会全力推荐使用的XDocument 又名的LINQ to XML。它的的简单的创建文档并进行处理。例如,它的之间的差:

If you get the choice, however, I would thoroughly recommend using XDocument aka LINQ to XML. It's much simpler to create documents and process them. For example, it's the difference between:

XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("root");
root.SetAttribute("name", "value");
XmlElement child = doc.CreateElement("child");
child.InnerText = "text node";
root.AppendChild(child);
doc.AppendChild(root);

XDocument doc = new XDocument(
    new XElement("root",
                 new XAttribute("name", "value"),
                 new XElement("child", "text node")));

命名空间是pretty容易LINQ与合作,XML,不像我见过的其他任何XML API:

Namespaces are pretty easy to work with in LINQ to XML, unlike any other XML API I've ever seen:

XNamespace ns = "http://somewhere.com";
XElement element = new XElement(ns + "elementName");
// etc

的LINQ to XML也确实很好用LINQ - 它的建设模式,您可以轻松真正建立与子元素的序列元素:

LINQ to XML also works really well with LINQ - its construction model allows you to build elements with sequences of sub-elements really easily:

// Customers is a List<Customer>
XElement customersElement = new XElement("customers",
    customers.Select(c => new XElement("customer",
        new XAttribute("name", c.Name),
        new XAttribute("lastSeen", c.LastOrder)
        new XElement("address",
            new XAttribute("town", c.Town),
            new XAttribute("firstline", c.Address1),
            // etc
    ));

这是所有有更多的声明,这与一般的LINQ风格的结合使用。

It's all a lot more declarative, which fits in with the general LINQ style.

现在的布兰农提到的,这些都是在内存中的API,而不是那些流(尽管 XStreamingElement 支持懒惰输出)。 的XmlReader 的XmlWriter 在.NET XML流的正常方式,但你可以混合所有API在一定程度上。例如,您可以流了大量文件,但通过定位一个的XmlReader 在一个元素开始,读取的XElement 从它和处理它,然后移动到下一个元素等,有关于这项技术的各种博客文章,<一个href=\"http://lennilobel.word$p$pss.com/2009/09/02/streaming-into-linq-to-xml-using-c-custom-iterators-and-xmlreader/\">here's一个我发现了一个快速搜索。

Now as Brannon mentioned, these are in-memory APIs rather than streaming ones (although XStreamingElement supports lazy output). XmlReader and XmlWriter are the normal ways of streaming XML in .NET, but you can mix all the APIs to some extent. For example, you can stream a large document but use LINQ to XML by positioning an XmlReader at the start of an element, reading an XElement from it and processing it, then moving on to the next element etc. There are various blog posts about this technique, here's one I found with a quick search.

这篇关于的XDocument或XmlDocument的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆