简体   繁体   English

C#XML解析-搜索特定元素

[英]C# XML Parsing - Searching for specific elements

I'm using an XMLReader in C# .net 4.0 to search through a small snippet of XML. 我在C#.net 4.0中使用XMLReader来搜索一小段XML。 I want to find specific elements in the XML, and I'm using the XMLReader.ReadToFollowing("name") method to find the elements. 我想在XML中找到特定的元素,并且正在使用XMLReader.ReadToFollowing("name")方法来查找元素。 I do not know the order in the XML document that the elements are; 我不知道这些元素在XML文档中的顺序。 they be in a difference sequence or missing entirely. 它们处于差异顺序或完全缺失。 The order does not matter to me, but I will throw an exception if the element is missing. 顺序对我来说并不重要,但是如果缺少该元素,我将抛出异常。

The XMLReader is forward only, so if the first element I'm looking for is the last value in the XML document, additional reads will fail. XMLReader仅向前,因此,如果我要查找的第一个元素是XML文档中的最后一个值,则其他读取将失败。

I was thinking of creating a new XMLReader for each search. 我正在考虑为每个搜索创建一个新的XMLReader。 I only have a small set of elements to find and the XML isn't huge so I don't think there will be much overhead, but I could be wrong. 我只有很少的元素集,而且XML也不是很大,所以我认为不会有太多开销,但是我可能是错的。

Is there a better library to use for searching XML when you do not know the order of the elements? 当您不知道元素的顺序时,是否有更好的库可用于搜索XML? Or is a lack of order in XML a violation of the XML specification? 还是XML缺乏顺序违反了XML规范?

Specifically, let's say I have some simple XML. 具体来说,假设我有一些简单的XML。 A head and 10 Children. 一个头和十个孩子。

<sometag>
     <element1>data</element1>
     <element2>data</element2>
     .
     .
     .
     <element10>data</element10>
</sometag>

Would it be inefficient to open 5 XMLReaders to find those elements? 打开5个XMLReader来查找那些元素是否效率低下? I could also use the one reader and step through each element, but then I would need to keep track of which elements I've found. 我还可以使用一个阅读器并逐步遍历每个元素,但随后我需要跟踪找到的元素。

Instead of XMLReader you could use XDocument (linq2xml), do it fast and easy (by Element method): 可以使用XDocument (linq2xml)代替XMLReader,而快速又轻松地执行此操作(通过Element方法):

var doc = XDocument.Load(xmlFilePath);
var element1 = doc.Element(searchItemName);
if (element1 == null) throw ...
...
return ....;

You can call it as many time as you like without reloading xml file. 您可以根据需要多次调用它,而无需重新加载xml文件。

Try using XPath. 尝试使用XPath。 It is much faster and isn't sensitive to the order of elements (only the hierarchy). 它快得多,并且对元素的顺序(仅是层次结构)不敏感。

MSDN Documentation: http://msdn.microsoft.com/en-us/library/ms256086.aspx MSDN文档: http : //msdn.microsoft.com/zh-cn/library/ms256086.aspx

Programming with XPath does require a bit of learning. 使用XPath进行编程确实需要一些学习。 And if your XML specifies a namespace, you'll need to be sure to include that in your XPath query. 并且,如果您的XML指定了名称空间,则需要确保将其包含在XPath查询中。

您具有.NET 4.0,因此请使用Linq到XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM