I'm having a problem attaching a name space. I'm getting an xml
string of information as a result of a third party web service. I
can't change the format of the string.
The xml file looks like the following
<Definition xmlns="Aspentech.Batch21">
<Area name="CIPStation" description="CIP Station" ...
</Area>
<Area ...>
</Area>
</Definition>
I then want to read the xml into an XPathDocument so that I can
extract the the name attribute of the area node , sort it , and then
add it into a combo box.
strXmlResult = b21WebServ.xml(strXmlQuery);
XmlDocument xmldocAreas= new XmlDocument();
// first we load our xml into an XmlDocument
xmldocAreas.LoadXml(strXmlResult);
// then we read the xml into a node reader
XmlNodeReader xmlnodreadAreas = new XmlNodeReader(xmldocAreas);
// then we create an xpath document pointing it to the node
// reader which contains the xml
XPathDocument xpathdocAreas = new XPathDocument(xmlnodreadAreas);
// then we create a navigator so that we can add a sort method
XPathNavigator xpathnavAreas = xpathdocAreas.CreateNavigator();
// Ensure we are at the root node
xpathnavAreas.MoveToRoot();
// then we select are the areas in the xml
XPathExpression xpathexpAreas = null;
xpathexpAreas = xpathnavAreas.Compile("Definition/Area/@name");
XmlNamespaceManager ns = new
XmlNamespaceManager(xpathnavAreas.NameTable);
ns.AddNamespace("ns" ,"Aspentech.Batch21");
xpathexpAreas.SetContext(ns);
// then we iterate through the sorted result
XPathNodeIterator xpathnoditAreas =
xpathnavAreas.Select(xpathexpAreas);
while (xpathnoditAreas.MoveNext())
{
this.cboBatchArea.Items.Add(xpathnoditAreas.Curren t.Value);
.................
The problem is that this doesn't seem to work. However if I modify
the xml so that it looks like this (and hence load it from a file ...
)
<Definition xmlns:bk="Aspentech.Batch21">
<Area name="CIPStation" description="CIP Station" ...
</Area>
<Area ...>
</Area>
</Definition>
and instead use
ns.AddNamespace("bk" ,"Aspentech.Batch21");
then it does work.
I'm a bit baffled as to why this is. I would really appreciate some
help on this as I've been trying to solve it for the last few days
(tragic I know but only starting...) and have really hit a brick wall
with this one.