C# 如何解析 XML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55828/
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 does one parse XML files?
提问by domoaringatoo
Is there a simple method of parsing XML files in C#? If so, what?
有没有一种在 C# 中解析 XML 文件的简单方法?如果是这样,是什么?
采纳答案by Jon Galloway
I'd use LINQ to XMLif you're in .NET 3.5 or higher.
如果您使用 .NET 3.5 或更高版本,我会使用LINQ to XML。
回答by aku
I'm not sure whether "best practice for parsing XML" exists. There are numerous technologies suited for different situations. Which way to use depends on the concrete scenario.
我不确定是否存在“解析 XML 的最佳实践”。有许多适用于不同情况的技术。使用哪种方式取决于具体场景。
You can go with LINQ to XML, XmlReader
, XPathNavigator
or even regular expressions. If you elaborate your needs, I can try to give some suggestions.
您可以使用LINQ to XML、XmlReader
,XPathNavigator
甚至正则表达式。如果您详细说明您的需求,我可以尝试提供一些建议。
回答by Vinko Vrsalovic
Use XmlTextReader
, XmlReader
, XmlNodeReader
and the System.Xml.XPath
namespace. And (XPathNavigator
, XPathDocument
, XPathExpression
, XPathnodeIterator
).
使用XmlTextReader
,XmlReader
,XmlNodeReader
和System.Xml.XPath
命名空间。和 ( XPathNavigator
, XPathDocument
, XPathExpression
, XPathnodeIterator
)。
Usually XPath
makes reading XML easier, which is what you might be looking for.
通常XPath
使阅读 XML 更容易,这正是您可能正在寻找的。
回答by Ash
If you're using .NET 2.0, try XmlReader
and its subclasses XmlTextReader
, and XmlValidatingReader
. They provide a fast, lightweight (memory usage, etc.), forward-only way to parse an XML file.
如果您使用 .NET 2.0,请尝试XmlReader
及其子类XmlTextReader
, 和XmlValidatingReader
. 它们提供了一种快速、轻量(内存使用等)、只进的解析 XML 文件的方法。
If you need XPath
capabilities, try the XPathNavigator
. If you need the entire document in memory try XmlDocument
.
如果您需要XPath
功能,请尝试使用XPathNavigator
. 如果您需要内存中的整个文档,请尝试XmlDocument
.
回答by Lukas ?alkauskas
It's very simple. I know these are standard methods, but you can create your own library to deal with that much better.
这很简单。我知道这些是标准方法,但您可以创建自己的库来更好地处理这些问题。
Here are some examples:
这里有些例子:
XmlDocument xmlDoc= new XmlDocument(); // Create an XML document object
xmlDoc.Load("yourXMLFile.xml"); // Load the XML document from the specified file
// Get elements
XmlNodeList girlAddress = xmlDoc.GetElementsByTagName("gAddress");
XmlNodeList girlAge = xmlDoc.GetElementsByTagName("gAge");
XmlNodeList girlCellPhoneNumber = xmlDoc.GetElementsByTagName("gPhone");
// Display the results
Console.WriteLine("Address: " + girlAddress[0].InnerText);
Console.WriteLine("Age: " + girlAge[0].InnerText);
Console.WriteLine("Phone Number: " + girlCellPhoneNumber[0].InnerText);
Also, there are some other methodsto work with. For example, here. And I think there is no one best method to do this; you always need to choose it by yourself, what is most suitable for you.
此外,还有一些其他方法可以使用。例如,这里。我认为没有一种最好的方法可以做到这一点。你总是需要自己选择它,什么是最适合你的。
回答by David Schmitt
Use a good XSD Schemato create a set of classes with xsd.exeand use an XmlSerializer
to create a object tree out of your XML and vice versa. If you have few restrictions on your model, you could even try to create a direct mapping between you model classes and the XML with the Xml*Attributes.
使用良好的XSD 架构通过xsd.exe创建一组类,并使用 anXmlSerializer
从 XML 创建对象树,反之亦然。如果您对模型的限制很少,您甚至可以尝试使用 Xml*Attributes 在模型类和 XML 之间创建直接映射。
There is an introductory article about XML Serialisationon MSDN.
MSDN 上有一篇关于 XML 序列化的介绍性文章。
Performance tip: Constructing an XmlSerializer
is expensive. Keep a reference to your XmlSerializer
instance if you intend to parse/write multiple XML files.
性能提示:构建一个XmlSerializer
是昂贵的。XmlSerializer
如果您打算解析/编写多个 XML 文件,请保留对您的实例的引用。
回答by Simon Steele
If you're processing a large amount of data (many megabytes) then you want to be using XmlReader
to stream parse the XML.
如果您正在处理大量数据(许多兆字节),那么您希望使用XmlReader
流式解析 XML。
Anything else (XPathNavigator
, XElement
, XmlDocument
and even XmlSerializer
if you keep the full generated object graph) will result in high memory usageand also a very slow load time.
其他任何东西(XPathNavigator
, XElement
,XmlDocument
即使XmlSerializer
您保留完整生成的对象图)也会导致高内存使用率和非常慢的加载时间。
Of course, if you need all the data in memory anyway, then you may not have much choice.
当然,如果您无论如何都需要内存中的所有数据,那么您可能没有太多选择。
回答by Wojtpl2
You can use ExtendedXmlSerializerto serialize and deserialize.
您可以使用ExtendedXmlSerializer进行序列化和反序列化。
InstalationYou can install ExtendedXmlSerializer from nugetor run the following command:
安装您可以从nuget安装 ExtendedXmlSerializer或运行以下命令:
Install-Package ExtendedXmlSerializer
Serialization:
序列化:
ExtendedXmlSerializer serializer = new ExtendedXmlSerializer();
var obj = new Message();
var xml = serializer.Serialize(obj);
Deserialization
反序列化
var obj2 = serializer.Deserialize<Message>(xml);
Standard XML Serializer in .NET is very limited.
.NET 中的标准 XML 序列化程序非常有限。
- Does not support serialization of class with circular reference or class with interface property,
- Does not support Dictionaries,
- There is no mechanism for reading the old version of XML,
- If you want create custom serializer, your class must inherit from IXmlSerializable. This means that your class will not be a POCO class,
- Does not support IoC.
- 不支持带有循环引用的类或带有接口属性的类的序列化,
- 不支持字典,
- 没有读取旧版 XML 的机制,
- 如果您想创建自定义序列化程序,您的类必须从 IXmlSerializable 继承。这意味着您的课程将不是 POCO 课程,
- 不支持 IoC。
ExtendedXmlSerializer can do this and much more.
ExtendedXmlSerializer 可以做到这一点以及更多。
ExtendedXmlSerializer support .NET 4.5or higher and .NET Core. You can integrate it with WebApi and AspCore.
ExtendedXmlSerializer 支持.NET 4.5或更高版本和.NET Core。您可以将其与 WebApi 和 AspCore 集成。
回答by Tapan kumar
You can parse the XML using this library System.Xml.Linq
. Below is the sample code I used to parse a XML file
您可以使用此库解析 XML System.Xml.Linq
。下面是我用来解析 XML 文件的示例代码
public CatSubCatList GenerateCategoryListFromProductFeedXML()
{
string path = System.Web.HttpContext.Current.Server.MapPath(_xmlFilePath);
XDocument xDoc = XDocument.Load(path);
XElement xElement = XElement.Parse(xDoc.ToString());
List<Category> lstCategory = xElement.Elements("Product").Select(d => new Category
{
Code = Convert.ToString(d.Element("CategoryCode").Value),
CategoryPath = d.Element("CategoryPath").Value,
Name = GetCateOrSubCategory(d.Element("CategoryPath").Value, 0), // Category
SubCategoryName = GetCateOrSubCategory(d.Element("CategoryPath").Value, 1) // Sub Category
}).GroupBy(x => new { x.Code, x.SubCategoryName }).Select(x => x.First()).ToList();
CatSubCatList catSubCatList = GetFinalCategoryListFromXML(lstCategory);
return catSubCatList;
}
回答by Joel Harkes
In Addition you can use XPath selector in the following way (easy way to select specific nodes):
此外,您可以通过以下方式使用 XPath 选择器(选择特定节点的简便方法):
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
var found = doc.DocumentElement.SelectNodes("//book[@title='Barry Poter']"); // select all Book elements in whole dom, with attribute title with value 'Barry Poter'
// Retrieve your data here or change XML here:
foreach (XmlNode book in nodeList)
{
book.InnerText="The story began as it was...";
}
Console.WriteLine("Display XML:");
doc.Save(Console.Out);