Reading XML file which contains namespaces
Hi all,
I have an excel vba application which outputs some data as XML. I need to read this xml data in a .NET application. The file automatically contains several namespaces which I can't prevent. Here is the XML file (similified):
<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
<s:ElementType name='row' content='eltOnly'>
<s:AttributeType name='someNumber' rs:number='2' rs:nullable='true' rs:maydefer='true' rs:writeunknown='true'>
<s:datatype dt:type='i2' dt:maxLength='2' rs:precision='5' rs:fixedlength='true'/>
</s:AttributeType>
<s:AttributeType name='someText' rs:number='3' rs:nullable='true' rs:maydefer='true'>
<s:datatype dt:type='string' dt:maxLength='255'/>
</s:AttributeType>
<s:extends type='rs:rowbase'/>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row someNumber='1' someText='TextHere'/>
</rs:data>
</xml>
Usually there are loads of z:row elements, it is these that I need to get. The code I have written uses XPath to get to them, but is complicated by namespaces, and at the moment returns no rows at all. I load an XmlDocument called xmlDoc, and then my code is:
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsMgr.AddNamespace("", xmlDoc.DocumentElement.NamespaceURI);
nsMgr.AddNamespace("s", xmlDoc.DocumentElement.NamespaceURI);
nsMgr.AddNamespace("dt", xmlDoc.DocumentElement.NamespaceURI);
nsMgr.AddNamespace("rs", xmlDoc.DocumentElement.NamespaceURI);
nsMgr.AddNamespace("z", xmlDoc.DocumentElement.NamespaceURI);
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList nodes = root.SelectNodes(@"/xml/rs:data/z:row",nsMgr);
Whilst this doesn't generate an error, it also doesn't return any rows. Anyone know what I am doing wrong?
Thanks in advance!