XDocument 或 XmlDocument [英] XDocument or XmlDocument

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

问题描述

我现在正在学习 XmlDocument 但我刚刚遇到了 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")));

命名空间在 LINQ to 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.

现在正如 Brannon 所提到的,这些是内存中的 API,而不是流式 API(尽管 XStreamingElement 支持延迟输出).XmlReaderXmlWriter 是 .NET 中流式传输 XML 的常规方式,但您可以在一定程度上混合所有 API.例如,您可以流式传输一个大文档,但通过将 XmlReader 定位在元素的开头,从中读取一个 XElement 并处理它,然后移动到下一个元素等.关于这种技术有各种博客文章,这是我通过快速搜索找到的一个.

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天全站免登陆