473,804 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem parsing XML with a namespace

I have some XML that is being generated by an external component and
structurally looks like this:

<?xml version='1.0' ?>
<rootObject xmlns='http://company.com/root.xsd'>
<subElement>Val ue</subElement>
</rootObject>

The 'http://company.com/root.xsd' doesn't actually exist, but I didn't
think that mattered.. I have some utility functions that I use to help me
process the XML document, such as:

public static XmlNameSpaceMan ager nsm = null;
public static bool NodeExists(XmlN ode sNode, string sNodeXPath)
{
return (sNode.SelectSi ngleNode(sNodeX Path, nsm) != null);
}

and I would normally use it like:

string srcXML = "...the XML shown above...";
XMLDocument xmlDoc = new XMLDocument();
xmlDoc.LoadXML( srcXML);
bool b = NodeExists(xmlD ocument.Documen tElement, "/rootObject");

If the source XML has the 'xmlns' tag, then NodeExists returns'false',
whereas if I take out the xmlns attribute tag, it returns 'true'. I want
it to always return true (ignoring namespaces).

So a few questions:

1. Why is the added namespace causing the NodeExists to return false?
2. How can I get the SelectSingleNod e to perform as I need it to, ignoring
(or at least accounting for) the added namespace attribute?

Normally, I use this code with nsm set to null. (In fact, until just now,
I was using the SelectSingleNod e that didn't specify nsm at all.) I've
tried setting the 'nsm' variable just prior to calling NodeExists:

nsm = new XMLNameSpaceMan ager(xmlDoc.Nam eTable);

and I've also tried adding the namespace:

nsm.AddNamespac e(string.Empty, "http://company.com/root.xsd");

but neither of these seem to help...

What can I do??

-mdb

Jul 3 '06 #1
1 2462
A workaround when using xml with a default namespace declaration:

Add an alias (prefix) to the default namespace (e.g. "x" is this example):
nsm.AddNamespac e("x", "http://company.com/root.xsd");
(note: might want to read this dynamically from the xmlns attribute on the
doc-element to save hard-coding)

and then use that as a prefix on every xpath expression:
bool b = NodeExists(xmlD ocument.Documen tElement, "/x:rootObject");

Note that a longer xpath might be "x:Somethin g/x:SomethingElse/x:TheOther" -
you get the idea.

Hope this helps,

Marc
Jul 3 '06 #2

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

Similar topics

5
6391
by: Mike McGavin | last post by:
Hi everyone. I've been trying for several hours now to get minidom to parse namespaces properly from my stream of XML, so that I can use DOM methods such as getElementsByTagNameNS(). For some reason, though, it just doesn't seem to want to split the prefixes from the rest of the tags when parsing. The minidom documentation at http://docs.python.org/lib/module-xml.dom.minidom.html implies that
4
2464
by: Erik Moore | last post by:
I am both producing and parsing an xml document that needs to be validated against a schema. I wanted some consumers of the document to have the option of not performing a validation, so I left the nodes in the instance unqualified. An example of the document is below: <?xml version="1.0" encoding="utf-8" ?> <filingreceipt xmlns="http://www.disclosureusa.org/filingreceipt.xsd"> <cpofilingnumber>TX2004043037501</cpofilingnumber>...
5
4152
by: OJO | last post by:
Hello microsoft.public.dotnet.xml! I need to parse some 'jabber xml' (www.jabber.org). I opted for using System.Xml.XmxDocument. The sample 'jabber xml' goes here: <message xmlns='jabber:client' from='chat@server.com/user1' xml:lang='pl' type='groupchat' to='user2@server.com/res'> <body xmlns:xml='http://www.w3.org/XML/1998/namespace'> MESSAGE TEXT </body>
0
965
by: Kaush | last post by:
Hi all, I am implementing a web service server to accept SOAP messages from my client and send a SOAP response back. Actually I am using .NET's WSE-2 for receiving and sending SOAP messages. The message I get from my client has a "security" element with "username" and "password" fields which I got to authenticate. Actually the namespace associated with "security" element my client is using is...
10
409
by: Antoon Pardon | last post by:
I have the following little piece of code: class Cfg:pass #config = Cfg() def assign(): setattr(config, 'Start' , ) def foo(): config = Cfg()
1
1466
by: Alex Maghen | last post by:
I've been using my installed VS 2005 for several months with no problem. Suddenly, something strange is happeneing and I'm not sure if it's something I'm missing in ASP.NET or something that's gotten messed up in VS. If I create a completely new Web Site Project in a blank directory with nothing in it, VS automatically creates a single Default.aspx with a code-behind page. If I go in and edite the Code-Behind and insert something like a...
4
6845
by: Neil.Smith | last post by:
I can't seem to find any references to this, but here goes: In there anyway to parse an html/aspx file within an asp.net application to gather a collection of controls in the file. For instance what I'm trying to do is upload a html file onto the web server, convert it to aspx file and then parse it for input tags/controls, which in turn will become fields in a newly created database table. Clearly when the aspx file is called the...
12
3411
by: Julian | last post by:
Hi, I am having problems with a function that I have been using in my program to read sentences from a 'command file' and parse them into commands. the surprising thing is that the program works fine on some computers and not so fine on others. I tried debugging and cannot make any sense of it. I narrowed it down to the seekg function and made this simple program which (from what I understand) does not seem to be working as expected in all...
6
5945
by: jackwootton | last post by:
Hello everyone, I understand that XML can be parsed using JavaScript using the XML Document object. However, it is possible to parse XHTML using JavaScript? I currently listen for DOMMutation events, when the events occur I access the node which was inserted or removed (event.target). There is only ever about 5 lines of XHTML nested in the node, however it would be silly for me to parse it manually using methods like hasChildNodes...
2
2668
by: nicky123 | last post by:
Hi everyone, This is a brief description that I have provided for parsing & displaying an XML document using DOM API. Please feel free to post your own comments & views regarding this discussion. Thank you. The first step of parsing an XML document is to import the DOM API related classes such as :- java.io.* which contains all the interfaces to perform an I/O operation. org.xml.sax.* which contains all the interfaces...
0
9714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10600
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10350
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7638
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6866
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.