473,796 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

xPath does not work when the XML Document has xmlns

Hi,

I am receiving an XML document from other system, and I am using xPath to
search one of the node in the document. For example, the xml document like
this:

<Company>
<Employees>
<Employee ID="1">
<FirstName>Klau s</FirstName>
<LastName>Salch ner</LastName>
<PhoneNumber>41 0-727-5112</PhoneNumber>
<EmailAddress>k l************@h otmail.com</EmailAddress>
<WebAddress>htt p://www.enterprise-minds.com</WebAddress>
<JobTitle>Sr. Enterprise Architect</JobTitle>
</Employee>
<Employee ID="2">
<FirstName>Pete r</FirstName>
<LastName>Pan </LastName>
<PhoneNumber>60 4-111-1111</PhoneNumber>
<EmailAddress>p e*******@fictio n.com</EmailAddress>
<JobTitle>Sr. Developer</JobTitle>
</Employee>
</Employees>
</Company>

I can use following command to search the node:
XmlNode oNode1 =
oXML.SelectSing leNode("/descendant::Job Title[ancestor::Emplo yee/@ID=2]");

Where oXML is an XML document object that loaded above xml.

For some reason, the xml I received recently changed a little bit, an extra
xmlns attribute added to both Company and Employees Nodes, and my search
command does not work any more, can anybody give a solution? Thanks in
advance!

p.s. The new XML looks like this:

<Company xmlns="http://schemas.abc.com ">
<Employees xmlns="http://schemas.def.com ">
<Employee ID="1">
<FirstName>Klau s</FirstName>
<LastName>Salch ner</LastName>
<PhoneNumber>41 0-727-5112</PhoneNumber>
<EmailAddress>k l************@h otmail.com</EmailAddress>
<WebAddress>htt p://www.enterprise-minds.com</WebAddress>
<JobTitle>Sr. Enterprise Architect</JobTitle>
</Employee>
<Employee ID="2">
<FirstName>Pete r</FirstName>
<LastName>Pan </LastName>
<PhoneNumber>60 4-111-1111</PhoneNumber>
<EmailAddress>p e*******@fictio n.com</EmailAddress>
<JobTitle>Sr. Developer</JobTitle>
</Employee>
</Employees>
</Company>

Feb 22 '07 #1
2 4445
You need to add the namespaces to the XmlDocument object. You can find sample
code using NameSpaceManage r in overloaded constructors in the documentation
for the XmlDocument class.
Pete

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Jason Zhou" wrote:
Hi,

I am receiving an XML document from other system, and I am using xPath to
search one of the node in the document. For example, the xml document like
this:

<Company>
<Employees>
<Employee ID="1">
<FirstName>Klau s</FirstName>
<LastName>Salch ner</LastName>
<PhoneNumber>41 0-727-5112</PhoneNumber>
<EmailAddress>k l************@h otmail.com</EmailAddress>
<WebAddress>htt p://www.enterprise-minds.com</WebAddress>
<JobTitle>Sr. Enterprise Architect</JobTitle>
</Employee>
<Employee ID="2">
<FirstName>Pete r</FirstName>
<LastName>Pan </LastName>
<PhoneNumber>60 4-111-1111</PhoneNumber>
<EmailAddress>p e*******@fictio n.com</EmailAddress>
<JobTitle>Sr. Developer</JobTitle>
</Employee>
</Employees>
</Company>

I can use following command to search the node:
XmlNode oNode1 =
oXML.SelectSing leNode("/descendant::Job Title[ancestor::Emplo yee/@ID=2]");

Where oXML is an XML document object that loaded above xml.

For some reason, the xml I received recently changed a little bit, an extra
xmlns attribute added to both Company and Employees Nodes, and my search
command does not work any more, can anybody give a solution? Thanks in
advance!

p.s. The new XML looks like this:

<Company xmlns="http://schemas.abc.com ">
<Employees xmlns="http://schemas.def.com ">
<Employee ID="1">
<FirstName>Klau s</FirstName>
<LastName>Salch ner</LastName>
<PhoneNumber>41 0-727-5112</PhoneNumber>
<EmailAddress>k l************@h otmail.com</EmailAddress>
<WebAddress>htt p://www.enterprise-minds.com</WebAddress>
<JobTitle>Sr. Enterprise Architect</JobTitle>
</Employee>
<Employee ID="2">
<FirstName>Pete r</FirstName>
<LastName>Pan </LastName>
<PhoneNumber>60 4-111-1111</PhoneNumber>
<EmailAddress>p e*******@fictio n.com</EmailAddress>
<JobTitle>Sr. Developer</JobTitle>
</Employee>
</Employees>
</Company>

Feb 22 '07 #2
Thanks Pete, I will take a look.

Jason

"Peter Bromberg [C# MVP]" <pb*******@yaho o.yabbadabbadoo .comwrote in
message news:66******** *************** ***********@mic rosoft.com...
You need to add the namespaces to the XmlDocument object. You can find
sample
code using NameSpaceManage r in overloaded constructors in the
documentation
for the XmlDocument class.
Pete

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Jason Zhou" wrote:
>Hi,

I am receiving an XML document from other system, and I am using xPath to
search one of the node in the document. For example, the xml document
like
this:

<Company>
<Employees>
<Employee ID="1">
<FirstName>Klau s</FirstName>
<LastName>Salch ner</LastName>
<PhoneNumber>41 0-727-5112</PhoneNumber>
<EmailAddress>k l************@h otmail.com</EmailAddress>
<WebAddress>htt p://www.enterprise-minds.com</WebAddress>
<JobTitle>Sr. Enterprise Architect</JobTitle>
</Employee>
<Employee ID="2">
<FirstName>Pete r</FirstName>
<LastName>Pan </LastName>
<PhoneNumber>60 4-111-1111</PhoneNumber>
<EmailAddress>p e*******@fictio n.com</EmailAddress>
<JobTitle>Sr. Developer</JobTitle>
</Employee>
</Employees>
</Company>

I can use following command to search the node:
XmlNode oNode1 =
oXML.SelectSin gleNode("/descendant::Job Title[ancestor::Emplo yee/@ID=2]");

Where oXML is an XML document object that loaded above xml.

For some reason, the xml I received recently changed a little bit, an
extra
xmlns attribute added to both Company and Employees Nodes, and my search
command does not work any more, can anybody give a solution? Thanks in
advance!

p.s. The new XML looks like this:

<Company xmlns="http://schemas.abc.com ">
<Employees xmlns="http://schemas.def.com ">
<Employee ID="1">
<FirstName>Klau s</FirstName>
<LastName>Salch ner</LastName>
<PhoneNumber>41 0-727-5112</PhoneNumber>
<EmailAddress>k l************@h otmail.com</EmailAddress>
<WebAddress>htt p://www.enterprise-minds.com</WebAddress>
<JobTitle>Sr. Enterprise Architect</JobTitle>
</Employee>
<Employee ID="2">
<FirstName>Pete r</FirstName>
<LastName>Pan </LastName>
<PhoneNumber>60 4-111-1111</PhoneNumber>
<EmailAddress>p e*******@fictio n.com</EmailAddress>
<JobTitle>Sr. Developer</JobTitle>
</Employee>
</Employees>
</Company>


Feb 22 '07 #3

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

Similar topics

4
21210
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a native English speaker, it's quite hard to make the problem clear. Please see the following example.
4
8978
by: Gismo | last post by:
I have got file raport.rld which is an XML file generated by MS Reporting Services. The problem is: in this file are tags from two different namespaces xmlns="http://schemas.microsoft.com/sqlserver/reporting/2003/10/reportdefini tion" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"
3
1829
by: Bob | last post by:
Below is some code that use to work. Now it is not and I have yet to figure out why. I want to select the nodes to a nodelist and remove them from the dom. The only thing I can figure is it broke when I added my schema. XML <?xml version="1.0" encoding="utf-8"?><IADSNOTES xmlns="notes.xsd" count="4"><Note href="D:\V4.0\DEMO\IADS_4_0.sgm#IADS V4.0 TOC@0" csm="0"><User>Ralph</User><Title>IADS 4.0 Demo - Table of...
2
2882
by: ree32 | last post by:
When I import an xml document in Visual studio and Genereate as schema from it, and create a dataset from it, it adds this line into to the root element of my xml file - "xmlns="http://tempuri.org/nameOfRoot.xsd" I have no idea what its pointing to & what is tempuri.org? So when this tag is in my xml tag my xpath query never works. But when I delete it work fine.
3
3318
by: SD | last post by:
Hi, How can i get all the nodes with attribute Name = "Publisher" or Name="Administrator" using XPath query and C# for this xml doc? <GetRoleCollectionFromUser xmlns=\"http://schemas.microsoft.com/sharepoint/soap/directory/\"> <Roles> <Role ID=\"1073741826\" Name=\"Reader\" Description=\"....\" Type=\"2\" />
0
3947
by: Sebastian Gingter | last post by:
I have an infopath XML file and want to use XPath queries with the XmlDocument object. The Problem: Every (in my eyes correct) XPath query does return a NULL object. Example: XmlDocument doc = new XmlDocument(); doc.Load(fileName); XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
14
17205
by: Mikhail Teterin | last post by:
Hello! What's would be the syntax for a query, which would allow me to get only the elements with non-empty text-nodes? For example, from: <a><b></b></a> <c/> <d><e>meow</e></d>
0
9680
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
10230
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
10174
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7548
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
6788
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();...
1
4118
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
3
2926
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.