473,399 Members | 2,478 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,399 software developers and data experts.

SelectSingleNode with namespaces

My question is somewhat related to the previous question in this
newsgroup:

http://groups.google.de/group/micros...58771b3b60cd47

I am having trouble unerstanding how to use XmlNode.SelectSingleNode
Method (String, XmlNamespaceManager) node when an xmlns Atrribute is
present. this is my xml file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns="http://company.de/VCT9/Core/Configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://company.de/VCT9/Core/Configuration
config.xsd">
<logging>
<listeners>
<listener
name="myXMLListener"
outputFormat="XML"
targetType="File"
targetFileName="%source%_dd-MM-yy.log"
targeFileMaxSize=""
filterSources="AIS.*|SomeTarget"
filterEventTypes="All" />
<listener
name="myCSVListener"
outputFormat="CSV"
outputOptions="Callstack ThreadId"
targetFileName="dd-MM-yyyy.csv" />
</listeners>
</logging>
</configuration>

Now I am selecting the <listenersnode like this:

XmlDocument configurationDocument = new XmlDocument();
configurationDocument.Load(configFile);
XmlNamespaceManager manager = new
XmlNamespaceManager(configurationDocument.NameTabl e);
manager.AddNamespace("config",
"http://company.de/VCT9/Core/Configuration");
XmlNode listenersNode
= configurationDocument.SelectSingleNode
("//config:listeners",manager);

This works fine. There is only one problem, I seem to be forced to
hardcode the namespace:

manager.AddNamespace("config",
"http://company.de/VCT9/Core/Configuration");

I do not know the urn for the namespace
("http://company.de/VCT9/Core/Configuration") of the root node
<configurationat compiletime. It might change. How can I select the
<listenersnode without knowing the namespaceurn at compiletime.

While we are at it. Maybe it is possible to connect a xml-schema with a
xml file without having to specify a namespace
(xmlns="http://company.de/VCTC3/Core/Configuration") at all? If is how?

Oct 12 '06 #1
4 4391
Robert Ludig wrote:
I do not know the urn for the namespace
("http://company.de/VCT9/Core/Configuration") of the root node
<configurationat compiletime. It might change. How can I select the
<listenersnode without knowing the namespaceurn at compiletime.
//*[local-name()='listeners']
While we are at it. Maybe it is possible to connect a xml-schema with a
xml file without having to specify a namespace
(xmlns="http://company.de/VCTC3/Core/Configuration") at all? If is how?
You can do it programmatically. But that's weird. Namespace is usually
stable thing, why do you change it?

--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net
Oct 12 '06 #2


Robert Ludig wrote:

<configuration xmlns="http://company.de/VCT9/Core/Configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
XmlDocument configurationDocument = new XmlDocument();
configurationDocument.Load(configFile);
XmlNamespaceManager manager = new
XmlNamespaceManager(configurationDocument.NameTabl e);
manager.AddNamespace("config",
"http://company.de/VCT9/Core/Configuration");
XmlNode listenersNode
= configurationDocument.SelectSingleNode
("//config:listeners",manager);

This works fine. There is only one problem, I seem to be forced to
hardcode the namespace:

manager.AddNamespace("config",
"http://company.de/VCT9/Core/Configuration");
Assuming the namespace is declared on the root element (document
element) as a default namespace (xmlns="URL") then you can simply do e.g.
manager.AddNamespace("config",
configurationDocument.DocumentElement.NamespaceURI );

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 12 '06 #3
Assuming the namespace is declared on the root element ...

The assumtion I can make about the XML-Document, I have to parse is as
followed:

the namespace is either declared in the root element or no namespace
is declared at all (and no xsd is referenced).

What would be the correct approach in this case ?

Martin Honnen schrieb:
Robert Ludig wrote:

<configuration xmlns="http://company.de/VCT9/Core/Configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

XmlDocument configurationDocument = new XmlDocument();
configurationDocument.Load(configFile);
XmlNamespaceManager manager = new
XmlNamespaceManager(configurationDocument.NameTabl e);
manager.AddNamespace("config",
"http://company.de/VCT9/Core/Configuration");
XmlNode listenersNode
= configurationDocument.SelectSingleNode
("//config:listeners",manager);

This works fine. There is only one problem, I seem to be forced to
hardcode the namespace:

manager.AddNamespace("config",
"http://company.de/VCT9/Core/Configuration");

Assuming the namespace is declared on the root element (document
element) as a default namespace (xmlns="URL") then you can simply do e.g.
manager.AddNamespace("config",
configurationDocument.DocumentElement.NamespaceURI );

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 13 '06 #4


Robert Ludig wrote:

The assumtion I can make about the XML-Document, I have to parse is as
followed:

the namespace is either declared in the root element or no namespace
is declared at all (and no xsd is referenced).

What would be the correct approach in this case ?
Then you could check
if (yourDoc.DocumentElement.NamespaceURI == "") {
// use XPath expressions without a prefix and without a
// namespace manager here
}
else {
// set up namespace manager and prefix as
// already described and use that
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 13 '06 #5

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

Similar topics

6
by: Jay Bienvenu | last post by:
I have the following Visual Basic .NET code in an ASP.NET project: ' ... Dim xdStructure As XmlDocument Dim xnStructureRoot As XmlNode xdStructure = New XmlDocument...
3
by: 455 | last post by:
Hello all, I've been trying to figure this out for hours now... can anyone help? I have an XML document like this: <?xml version="1.0" encoding="utf-8" ?> <ICE:ServiceCall xmlns="ICE"...
2
by: adam | last post by:
I tried so many ways to select the node but its not working, please help. I want to research by the identifier in imsmanifest.xml file, the node could be item or resource. XmlDocument doc = new...
1
by: RedEagle | last post by:
Hi all! I have been dealing with a really strange problem with this xml file: <?xml version="1.0" encoding="UTF-8" ?> <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">...
6
by: David Thielen | last post by:
Hi; I am calling SelectSingleNode("/xml/s:Schema/s:ElementType/@name") where "/xml/s:Schema/s:ElementType/@name is a legit xpath statement (xml is the name of the rootnode) and that xpath...
3
by: Jonathan | last post by:
Hi, I use a XML-Doc with a Namespace like the following example: <HomedResources xmlns=http://schemas.microsoft.com/RtcServer/2002/11/dbimpexp Version="3"> <HomedResource...
1
by: Angela | last post by:
I am getting an error when I attempt to access a node with SelectSingleNode(): "The expression passed to this method should result in a NodeSet." I understand there is some confusion when you have...
19
by: David Thielen | last post by:
Hi; If there are no namespaces this works fine for me. But if the xml has namespaces, then I get either no node back or an exception. Here is the sample xml: <root xmlns="http://www.test.org"...
5
by: Paul | last post by:
I have some code that loads and queries the web config file in the design enviroment. This worked happily pre-VS2005 and indeed is still able to load the Xml into an XmlDocument object. I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.