简体   繁体   English

在大型XML文档中查找特定属性

[英]Finding specific attributes in a large XML document

I have a large XML document that is around 100mb. 我有一个大约100mb的大型XML文档。 I need to find attributes for two tags in this document. 我需要在本文档中找到两个标签的属性。 I can do this by using similar code to the following: 我可以通过使用类似的代码来执行此操作:

XmlDocument xmlDocument = new XmlDocument ( );
xmlDocument.Load ( "C:\\myxml.xml" );

XmlNode node1 = xmlDocument.SelectSingleNode ( "/data/objects[@type='data type 1']" );
if ( null != node1 )
{
   result = node1 [ "Version" ].Value;
}

But doing so loads the entire XML into memory which seems to take around 200mb. 但这样做会将整个XML加载到内存中,这似乎需要大约200mb。 Is there anyway I can make this more efficient? 无论如何我可以提高效率吗?

Edit: Lots of nice answers using the XmlTextReader which I have written my code to use now. 编辑:很多很好的答案使用XmlTextReader我已经编写了我的代码现在使用。 (It will be more memory efficient, but ugly :). (这将是更高的内存效率,但丑陋:)。

For performance, SAX is much better than DOM since you actually need only one value. 为了提高性能,SAX比DOM要好得多,因为实际上只需要一个值。 SAX implementation in .NET Framework is XmlTextReader . .NET Framework中的SAX实现是XmlTextReader

You should try to use an XmlReader. 您应该尝试使用XmlReader。

From MSDN : 来自MSDN

Like the SAX reader, the XmlReader is a forward-only, read-only cursor. 与SAX阅读器一样,XmlReader是一个只进,只读游标。 It provides fast, non-cached stream access to the input. 它提供对输入的快速,非缓存流访问。 It can read a stream or a document. 它可以读取流或文档。 It allows the user to pull data, and skip records of no interest to the application. 它允许用户提取数据,并跳过对应用程序不感兴趣的记录。 The big difference lies in the fact that the SAX model is a "push" model, where the parser pushes events to the application, notifying the application every time a new node has been read, while applications using XmlReader can pull nodes from the reader at will. 最大的区别在于SAX模型是一个“推送”模型,解析器将事件推送到应用程序,每次读取新节点时通知应用程序,而使用XmlReader的应用程序可以从读取器读取节点将。

An example here . 这里有一个例子。

You can use the XmlReader class to do this. 您可以使用XmlReader类来执行此操作。 A simple but working example that does the same as your code above looks like this: 一个简单但有效的示例与上面的代码相同,如下所示:

string result = null;

using (var reader = XmlReader.Create(@"c:\\myxml.xml"))
{
    while (reader.Read())
    {
        if (reader.NodeType == XmlNodeType.Element
            && reader.Depth == 1
            && reader.LocalName == "objects"
            && reader.GetAttribute("type") == "data type 1")
        {
            result = reader.GetAttribute("Version");
            break;
        }
    }
}

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

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