Re: Random access of XML file
From: Stuart Celarier (stuart)
Date: 11/14/04
- Previous message: Stuart Celarier: "Re: XmlText Reader with a Network stream fails after SP1"
- In reply to: Martin Honnen: "Re: Random access of XML file"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 13 Nov 2004 18:10:41 -0800
As Martin suggested, you can use the XmlNode.SelectNodes method [1] to
select all nodes matching an XPath expression [2]. XmlNode.SelectSingleNode
selects the first node (in document order) that matches the XPath
expression.
The XmlDocument.GetElementByID method requires use of a DTD to identify
which attributes are treated as IDs [3]. DTDs are very nearly dead, and you
are better served in the long term by not relying on them.
If your document is not well-formed XML, then all bets are off.
Fundamentally, if it's not well-formed XML, then it isn't XML, period. In
the case of XmlDocument, you can't load a document that is not well-formed
XML.
If you want to go a step further beyond XPath support (SelectNodes), you can
look at XSLT [4], but that may be overkill depending on your needs. The XSLT
element <xsl:key> acts like an index over the XML source document, and the
key() function acts like a SELECT statement over that index. This has the
potential of delivering better performance.
Here is a key that indexes all elements having at least one attribute whose
name contains the string 'ID', indexed by the values of any such attributes.
<xsl:key name="AllIDs"
match="*[contains(local-name(@*), 'ID')]"
use="@*[contains(local-name(.), 'ID')]"/>
That key can be used in expressions in XSLT elements like this
key('AllIDs', '3')
This selects all elements anywhere in the document that have at least one
attribute whose name contains the string 'ID' and whose value is 3. (Caveat,
I'm just writing this XSLT off the top of my head to give you the flavor.)
I should point out that XSLT is a general purpose transform language for XML
and can take a while to learn. It may be much more than you need or are
willing to undertake.
Cheers,
Stuart Celarier, Fern Creek
[1]
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemxmlxmlnodeclassselectnodestopic1.asp
[2] http://www.w3.org/TR/1999/REC-xpath-19991116
[3]
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemxmlxmldocumentclassgetelementbyidtopic.asp
[4] http://www.w3.org/TR/1999/REC-xslt-19991116
- Previous message: Stuart Celarier: "Re: XmlText Reader with a Network stream fails after SP1"
- In reply to: Martin Honnen: "Re: Random access of XML file"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|