all groups > dotnet xml > october 2004 >
You're in the

dotnet xml

group:

AddNamespace where uri is not a http address and where there is no prefix


AddNamespace where uri is not a http address and where there is no prefix sarah.king NO[at]SPAM orb-is.com
10/28/2004 12:53:41 AM
dotnet xml:
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.Current.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
Re: AddNamespace where uri is not a http address and where there is no prefix S
10/28/2004 6:33:13 AM


Thanks Oleg, that worked an absolute treat.

In answer to your query, the reason I'm using XPathDocument when I
already have an XmlDocument is because I want to apply a sort criteria
to the Xml before I add it to the combo box.

When I looked up the AddSort method I got the impression that it only
applied to the XPath Document. Does it also apply to an XmlDocument?



*** Sent via Developersdex http://www.developersdex.com ***
Re: AddNamespace where uri is not a http address and where there is no prefix Oleg Tkachenko [MVP]
10/28/2004 2:35:26 PM
[quoted text, click to view]

What for? If you have info in XmlDocument already, what for to dublicate
it in XPathDocument? You can use XPath with XmlDocument in the same way
as with XPathDocument.

[quoted text, click to view]

That won't work. All elements in your XML are in "Aspentech.Batch21"
namespace. To select them using XPath you have to bind this namespace to
some prefix and use that prefix refering to element names:

[quoted text, click to view]

xpathexpAreas = xpathnavAreas.Compile("ns:Definition/ns:Area/@name");

Read some more about namespaces, e.g. "XML Namespaces and How They
Affect XPath and XSLT" at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml05202002.asp

--
Oleg Tkachenko [XML MVP]
Re: AddNamespace where uri is not a http address and where there is no prefix Oleg Tkachenko [MVP]
10/28/2004 3:43:07 PM
[quoted text, click to view]

In fact it applies to XPathNavigator. And both XPathDocument and
XmlDocument implement IXPathNavigable so are equal for this matter.

--
Oleg Tkachenko [XML MVP]
AddThis Social Bookmark Button