Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# XPath id() not working?

Tags:

c#

I'm using C# and I'm stumped. Does it just not support id()? I have a large XML file, about 4-5 of them at ~400kb, so I need some speed and performance wherever I can get it.

I use XmlDocument.SelectSingleNode("id('blahblahblah')") and it doesn't get the node by id. Am I going crazy or is it that C# XPath just doesn't support id()?

like image 985
Iggyhopper Avatar asked Jul 16 '26 16:07

Iggyhopper


1 Answers

Use XmlDocument.GetElementById to get the XmlElement with the specified ID, e.g.:

XmlElement elem = doc.GetElementById("blahblahblah");

This works only with documents specifying a DTD though:

Attributes with the name "ID" are not of type ID unless so defined in the DTD.


In case your document does not have a DTD, you could use an XPath expression to select the node with the id attribute set to your ID:

XmlElement elem = doc.SelectSingleNode("//*[@id='blahblahblah']");
like image 123
dtb Avatar answered Jul 18 '26 07:07

dtb