I cannot replicate this using an XML file
<MT>
<Events>
<event id="1">
<field name="blah" value="a_value" type="atype" />
</event>
</Events>
</MT>
And code
XmlDocument doc = new XmlDocument();
doc.Load(@"C:est.xml");
XmlNode node = doc.SelectSingleNode("//event[@id='1']");
This returns a non-null node as expected.
Update
After adding a xmlns="example.org"
to the <MT>
element, I had to configure a namespace manager for the XPath and use the namespace for the event. Couldn't get the default namespace to work for some reason.
XmlDocument doc = new XmlDocument();
doc.Load(@"D:est.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("e", "http://example.org");
XmlNode node = doc.SelectSingleNode("//e:event[@id='1']", manager);
One thing confused me when trying to get this to work. Why does XmlNamespaceManager need XmlNameTable from the document if not for finding out what namespaces it contains? As in, why do I need to define the NameTable and the namespace? I'd appreciate if someone who knows could drop a short comment.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…