473,385 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

S
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.
Nov 12 '05 #1
3 2979
S wrote:
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
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.
xpathexpAreas = xpathnavAreas.Compile("Definition/Area/@name");
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:

XmlNamespaceManager ns = new
XmlNamespaceManager(xpathnavAreas.NameTable);

ns.AddNamespace("ns" ,"Aspentech.Batch21");


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/de...ml05202002.asp

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2
S


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 ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #3
S wrote:
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?


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

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: palsuddeath | last post by:
Hello all! I'm no programmer, but I have to try to complete some code for a project that we are working on. There will be a web page that is set up to register devices online. Part of the...
0
by: Timo | last post by:
If there's a better place to ask, please advise. I've serialized a custom collection class using the XMLSerializer, and now I'd like to deserialize it. The xml representation of the serialized...
5
by: Kevin Newman | last post by:
Does anyone know of any application (AJAX or other) that will display the appropriate address for for the selected (or detected) country? If not, does anyone know where I can find a list or...
5
by: editor | last post by:
Newbie question: I've got an ASP that is accessing a db field populated with web addresses. The web addresses in the db do not have http:// as a prefix and as such the resulting pages do not...
1
by: Learner | last post by:
Hi there, I have installed Sql server 2005 developer on my machine which already has a Sql server 2000 installed on. Now i am trying to query the Sqlserver 2005 data(Ex: from Person.Address...
0
by: Shai Halevi | last post by:
I'm running the following script under PHP 4.4.2 in safe-mode: <?php header('WWW-Authenticate: Basic realm="some realm"'); header("HTTP/1.0 401 Unauthorized"); exit('The realm is supposed to be...
8
by: ajos | last post by:
hi frnds, the situation is im developing a web application in struts-- my specifications-eclipse3.2.2,struts1.1,tomcat5.5.23 mysql5.0 db. the scenario-i have a jsp page which adds the...
1
by: Berryl Hesh | last post by:
PROBLEM: I want to create a linq query of an in memory object based on 3 possible filters, and the only way I can think is something like the code below. Besides being verbose, I'm repeating...
1
by: Berryl Hesh | last post by:
PROBLEM: I want to create a linq query of an in memory object based on 3 possible filters, and the only way I can think is something like the code below. Besides being verbose, I'm repeating...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.