C#中如何处理XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/220867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to deal with XML in C#
提问by Malik Daud Ahmad Khokhar
What is the best way to deal with XML documents, XSD etc in C# 2.0?
在 C# 2.0 中处理 XML 文档、XSD 等的最佳方法是什么?
Which classes to use etc. What are the best practices of parsing and making XML documents etc.
使用哪些类等。解析和制作 XML 文档等的最佳实践是什么?
EDIT: .Net 3.5 suggestions are also welcome.
编辑:也欢迎 .Net 3.5 建议。
采纳答案by nyxtom
The primary means of reading and writing in C# 2.0 is done through the XmlDocumentclass. You can load most of your settings directly into the XmlDocument through the XmlReader it accepts.
在 C# 2.0 中读写的主要方式是通过XmlDocument类完成的。您可以通过它接受的 XmlReader 将大部分设置直接加载到 XmlDocument 中。
Loading XML Directly
直接加载 XML
XmlDocument document = new XmlDocument();
document.LoadXml("<People><Person Name='Nick' /><Person Name='Joe' /></People>");
Loading XML From a File
从文件加载 XML
XmlDocument document = new XmlDocument();
document.Load(@"C:\Path\To\xmldoc.xml");
// Or using an XmlReader/XmlTextReader
XmlReader reader = XmlReader.Create(@"C:\Path\To\xmldoc.xml");
document.Load(reader);
I find the easiest/fastest way to read an XML document is by using XPath.
我发现阅读 XML 文档最简单/最快的方法是使用 XPath。
Reading an XML Document using XPath (Using XmlDocument which allows us to edit)
使用 XPath 读取 XML 文档(使用允许我们编辑的 XmlDocument)
XmlDocument document = new XmlDocument();
document.LoadXml("<People><Person Name='Nick' /><Person Name='Joe' /></People>");
// Select a single node
XmlNode node = document.SelectSingleNode("/People/Person[@Name = 'Nick']");
// Select a list of nodes
XmlNodeList nodes = document.SelectNodes("/People/Person");
If you need to work with XSD documents to validate an XML document you can use this.
如果您需要使用 XSD 文档来验证 XML 文档,您可以使用它。
Validating XML Documents against XSD Schemas
根据 XSD 架构验证 XML 文档
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidateType = ValidationType.Schema;
settings.Schemas.Add("", pathToXsd); // targetNamespace, pathToXsd
XmlReader reader = XmlReader.Create(pathToXml, settings);
XmlDocument document = new XmlDocument();
try {
document.Load(reader);
} catch (XmlSchemaValidationException ex) { Trace.WriteLine(ex.Message); }
Validating XML against XSD at each Node (UPDATE 1)
在每个节点上针对 XSD 验证 XML(更新 1)
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidateType = ValidationType.Schema;
settings.Schemas.Add("", pathToXsd); // targetNamespace, pathToXsd
settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler);
XmlReader reader = XmlReader.Create(pathToXml, settings);
while (reader.Read()) { }
private void settings_ValidationEventHandler(object sender, ValidationEventArgs args)
{
// e.Message, e.Severity (warning, error), e.Error
// or you can access the reader if you have access to it
// reader.LineNumber, reader.LinePosition.. etc
}
Writing an XML Document (manually)
编写 XML 文档(手动)
XmlWriter writer = XmlWriter.Create(pathToOutput);
writer.WriteStartDocument();
writer.WriteStartElement("People");
writer.WriteStartElement("Person");
writer.WriteAttributeString("Name", "Nick");
writer.WriteEndElement();
writer.WriteStartElement("Person");
writer.WriteStartAttribute("Name");
writer.WriteValue("Nick");
writer.WriteEndAttribute();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
(UPDATE 1)
(更新 1)
In .NET 3.5, you use XDocument to perform similar tasks. The difference however is you have the advantage of performing Linq Queries to select the exact data you need. With the addition of object initializers you can create a query that even returns objects of your own definition right in the query itself.
在 .NET 3.5 中,您可以使用 XDocument 来执行类似的任务。但是,不同之处在于您可以执行 Linq 查询来选择所需的确切数据。通过添加对象初始值设定项,您可以创建一个查询,该查询甚至可以在查询本身中返回您自己定义的对象。
XDocument doc = XDocument.Load(pathToXml);
List<Person> people = (from xnode in doc.Element("People").Elements("Person")
select new Person
{
Name = xnode.Attribute("Name").Value
}).ToList();
(UPDATE 2)
(更新 2)
A nice way in .NET 3.5 is to use XDocument to create XML is below. This makes the code appear in a similar pattern to the desired output.
.NET 3.5 中的一个好方法是使用 XDocument 创建 XML,如下所示。这使得代码以与所需输出类似的模式出现。
XDocument doc =
new XDocument(
new XDeclaration("1.0", Encoding.UTF8.HeaderName, String.Empty),
new XComment("Xml Document"),
new XElement("catalog",
new XElement("book", new XAttribute("id", "bk001"),
new XElement("title", "Book Title")
)
)
);
creates
创造
<!--Xml Document-->
<catalog>
<book id="bk001">
<title>Book Title</title>
</book>
</catalog>
All else fails, you can check out this MSDN article that has many examples that I've discussed here and more. http://msdn.microsoft.com/en-us/library/aa468556.aspx
所有其他方法都失败了,您可以查看这篇 MSDN 文章,其中包含我在此处讨论过的许多示例以及更多示例。 http://msdn.microsoft.com/en-us/library/aa468556.aspx
回答by Marc Gravell
It depends on the size; for small to mid size xml, a DOM such as XmlDocument(any C#/.NET versions) or XDocument(.NET 3.5/C# 3.0) is the obvious winner. For using xsd, You can load xml using an XmlReader, and an XmlReader accepts (to Create) an XmlReaderSettings. The XmlReaderSettings objects has a Schemasproperty that can be used to perform xsd (or dtd) validation.
这取决于大小;对于中小型 xml,诸如XmlDocument(任何 C#/.NET 版本)或XDocument(.NET 3.5/C# 3.0)之类的 DOM是明显的赢家。对于使用 xsd,您可以使用XmlReader加载 xml ,并且 XmlReader 接受(以创建)XmlReaderSettings。XmlReaderSettings 对象具有可用于执行 xsd(或 dtd)验证的Schemas属性。
For writing xml, the same things apply, noting that it is a little easier to lay out content with LINQ-to-XML (XDocument) than the older XmlDocument.
对于编写 xml,同样适用,注意使用 LINQ-to-XML (XDocument) 布置内容比旧的 XmlDocument 更容易一些。
However, for huge xml, a DOM may chomp too much memory, in which case you might need to use XmlReader/XmlWriter directly.
但是,对于庞大的 xml,DOM 可能会占用太多内存,在这种情况下,您可能需要直接使用 XmlReader/XmlWriter。
Finally, for manipulating xml you may wish to use XslCompiledTransform(an xslt layer).
最后,为了操作 xml,您可能希望使用XslCompiledTransform(一个 xslt 层)。
The alternative to working with xml is to work with an object model; you can use xsd.exeto create classes that represent an xsd-compliant model, and simply load the xml as objects, manipulate it with OO, and then serialize those objects again; you do this with XmlSerializer.
使用 xml 的替代方法是使用对象模型;您可以使用xsd.exe创建代表 xsd 兼容模型的类,只需将 xml作为对象加载,使用 OO 操作它,然后再次序列化这些对象;你用XmlSerializer做到这一点。
回答by hurst
First of all, get to know the new XDocumentand XElementclasses, because they are an improvement over the previous XmlDocument family.
首先,了解新的XDocument和XElement类,因为它们是对以前的 XmlDocument 系列的改进。
- They work with LINQ
- They are faster and more lightweight
- 他们使用 LINQ
- 它们更快更轻
However, you may have to still use the old classes to work with legacy code - particularly previously generated proxies. In that case, you will need to become familiar with some patterns for interoperating between these XML-handling classes.
但是,您可能仍然需要使用旧类来处理遗留代码 - 特别是以前生成的代理。在这种情况下,您需要熟悉一些在这些 XML 处理类之间进行互操作的模式。
I think your question is quite broad, and would require too much in a single answer to give details, but this is the first general answer I thought of, and serves as a start.
我认为您的问题非常广泛,并且需要在单个答案中提供太多细节才能提供详细信息,但这是我想到的第一个通用答案,并且可以作为一个开始。
回答by Aaron Powell
If you're working in .NET 3.5 and you aren't affraid of experimental code you can check out LINQ to XSD (http://blogs.msdn.com/xmlteam/archive/2008/02/21/linq-to-xsd-alpha-0-2.aspx) which will generate .NET classes from an XSD (including built in rules from the XSD).
如果您使用 .NET 3.5 并且不害怕实验代码,则可以查看 LINQ to XSD ( http://blogs.msdn.com/xmlteam/archive/2008/02/21/linq-to- xsd-alpha-0-2.aspx),它将从 XSD 生成 .NET 类(包括来自 XSD 的内置规则)。
It then has the ability to write straight out to a file and read from a file ensuring that it conforms to the XSD rules.
然后它能够直接写入文件并从文件中读取,确保它符合 XSD 规则。
I definately suggest having an XSD for any XML document you work with:
我绝对建议为您处理的任何 XML 文档使用 XSD:
- Allows you to enforce rules in the XML
- Allows others to see how the XML is/ will be structured
- Can be used for validation of XML
- 允许您在 XML 中强制执行规则
- 允许其他人查看 XML 的结构/将如何构建
- 可用于 XML 的验证
I find that Liquid XML Studio is a great tool for generating XSD's and it's free!
我发现 Liquid XML Studio 是一个很好的生成 XSD 的工具,而且它是免费的!
回答by emremp
101 Linq samples
101 个 Linq 样本
http://msdn.microsoft.com/en-us/library/bb387098.aspx
http://msdn.microsoft.com/en-us/library/bb387098.aspx
and Linq to XML samples
和Linq to XML 示例
http://msdn.microsoft.com/en-us/vbasic/bb688087.aspx
http://msdn.microsoft.com/en-us/vbasic/bb688087.aspx
And I think Linq makes XML easy.
而且我认为 Linq 使 XML 变得简单。
回答by Peter C
If you create a typed dataset in the designer then you automatically get an xsd, a strongly typed object, and can load and save the xml with one line of code.
如果您在设计器中创建类型化数据集,那么您会自动获得一个 xsd,一个强类型对象,并且可以使用一行代码加载和保存 xml。
回答by Robert Rossney
nyxtom's answer is very good. I'd add a couple of things to it:
nyxtom 的回答非常好。我要添加一些东西:
If you need read-only access to an XML document, XPathDocument
is a much lighter-weight object than XmlDocument
.
如果您需要对 XML 文档的只读访问,XPathDocument
则是一个比XmlDocument
.
The downside of using XPathDocument
is that you can't use the familiar SelectNodes
and SelectSingleNode
methods of XmlNode
. Instead, you have to use the tools that the IXPathNavigable
provides: use CreateNavigator
to create an XPathNavigator
, and use the XPathNavigator
to create XPathNodeIterator
s to iterate over the lists of nodes you find via XPath. This generally requires a few more lines of code than the XmlDocument
methods.
使用的缺点XPathDocument
是不能使用熟悉的SelectNodes
和SelectSingleNode
方法XmlNode
。相反,您必须使用 提供的工具IXPathNavigable
:用于CreateNavigator
创建XPathNavigator
,并使用XPathNavigator
来创建XPathNodeIterator
以迭代您通过 XPath 找到的节点列表。这通常需要比XmlDocument
方法多几行代码。
But: the XmlDocument
and XmlNode
classes implement IXPathNavigable
, so any code you write to use those methods on an XPathDocument
will also work on an XmlDocument
. If you get used to writing against IXPathNavigable
, your methods can work against either object. (This is why using XmlNode
and XmlDocument
in method signatures is flagged by FxCop.)
但是:XmlDocument
和XmlNode
类实现了IXPathNavigable
,因此您编写的用于在 上使用这些方法的任何代码XPathDocument
也可以在XmlDocument
. 如果您习惯了针对 编写IXPathNavigable
,则您的方法可以针对任一对象工作。(这就是FxCop 标记usingXmlNode
和XmlDocument
in 方法签名的原因。)
Lamentably, XDocument
and XElement
(and XNode
and XObject
) don't implement IXPathNavigable
.
可悲的是,XDocument
and XElement
(and XNode
and XObject
) 没有实现IXPathNavigable
.
Another thing not present in nyxtom's answer is XmlReader
. You generally use XmlReader
to avoid the overhead of parsing the XML stream into an object model before you begin processing it. Instead, you use an XmlReader
to process the input stream one XML node at a time. This is essentially .NET's answer to SAX. It lets you write very fast code for processing very large XML documents.
nyxtom 的回答中没有的另一件事是XmlReader
. XmlReader
在开始处理 XML 流之前,您通常使用它来避免将 XML 流解析为对象模型的开销。相反,您使用 an 一次XmlReader
处理一个 XML 节点的输入流。这本质上是 .NET 对 SAX 的回答。它使您可以编写非常快的代码来处理非常大的 XML 文档。
XmlReader
also provides the simplest way of processing XML document fragments, e.g. the stream of XML elements with no encluding element that SQL Server's FOR XML RAW option returns.
XmlReader
还提供了处理 XML 文档片段的最简单方法,例如 SQL Server 的 FOR XML RAW 选项返回的没有包含元素的 XML 元素流。
The code you write using XmlReader
is generally very tightly coupled to the format of the XML it's reading. Using XPath allows your code to be much, much more loosely coupled to the XML, which is why it's generally the right answer. But when you need to use XmlReader
, you really need it.
您编写的代码XmlReader
通常与它所读取的 XML 的格式紧密耦合。使用 XPath 可以让您的代码与 XML 的耦合更加松散,这就是为什么它通常是正确的答案。但是当你需要使用时XmlReader
,你真的需要它。
回答by Steve Horn
Cookey's answer is good... but here are detailed instructions on how to create a strongly typed object from an XSD(or XML) and serialize/deserialize in a few lines of code:
Cookie 的回答很好......但这里有关于如何从 XSD(或 XML)创建强类型对象并在几行代码中序列化/反序列化的详细说明:
回答by Ryan Lundy
My personal opinion, as a C# programmer, is that the best way to deal with XML in C# is to delegate that part of the code to a VB .NET project. In .NET 3.5, VB .NET has XML Literals, which make dealing with XML much more intuitive. See here, for example:
作为 C# 程序员,我个人的观点是,在 C# 中处理 XML 的最佳方法是将这部分代码委托给 VB .NET 项目。在 .NET 3.5 中,VB .NET 具有 XML 文字,这使得处理 XML 更加直观。参见这里,例如:
Overview of LINQ to XML in Visual Basic
Visual Basic 中的 LINQ to XML 概述
(Be sure to set the page to display VB code, not C# code.)
(请务必将页面设置为显示 VB 代码,而不是 C# 代码。)
I'd write the rest of the project in C#, but handle the XML in a referenced VB project.
我会用 C# 编写项目的其余部分,但在引用的 VB 项目中处理 XML。
回答by mokumaxCraig
nyxtom,
纽约客,
Shouldn't "doc" and "xdoc" match in Example 1?
示例 1 中的“doc”和“xdoc”不应该匹配吗?
XDocument **doc** = XDocument.Load(pathToXml);
List<Person> people = (from xnode in **xdoc**.Element("People").Elements("Person")
select new Person
{
Name = xnode.Attribute("Name").Value
}).ToList();