"s:" in xpath causes error

E

Emily

Hi all,

I have the following simplified version of xml file:

<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'>
<s:Schema id='RowsetSchema'>
<s:ElementType name='row'>
<s:AttributeType name='Fund1'>
<s:AttributeType name='Fund2'>
<s:AttributeType name='Fund3'>
</s:ElementType>
</s:Schema>
</xml>

In my C# code, I use xpath as follows:

XmlNodeList items = document.SelectNodes
("//s:Schema/s:ElementType/s:AttributeType");

This causes runtime error, "An unhandled exception of type
'System.Xml.XPath.XPathException' occurred in system.xml.dll"

Any advice on how to work-around the prefix of "s:" in the xpath? Many
thanks!
 
C

Clive Dixon

Try using the overload taking an additional XmlNamespaceManager argument,
something like.

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'");

XmlNodeList items =
document.SelectNodes("//s:Schema/s:ElementType/s:AttributeType",nsmgr);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top