xPath does not work when the XML Document has xmlns

J

Jason Zhou

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>Klaus</FirstName>
<LastName>Salchner</LastName>
<PhoneNumber>410-727-5112</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<WebAddress>http://www.enterprise-minds.com</WebAddress>
<JobTitle>Sr. Enterprise Architect</JobTitle>
</Employee>
<Employee ID="2">
<FirstName>Peter</FirstName>
<LastName>Pan</LastName>
<PhoneNumber>604-111-1111</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<JobTitle>Sr. Developer</JobTitle>
</Employee>
</Employees>
</Company>

I can use following command to search the node:
XmlNode oNode1 =
oXML.SelectSingleNode("/descendant::JobTitle[ancestor::Employee/@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>Klaus</FirstName>
<LastName>Salchner</LastName>
<PhoneNumber>410-727-5112</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<WebAddress>http://www.enterprise-minds.com</WebAddress>
<JobTitle>Sr. Enterprise Architect</JobTitle>
</Employee>
<Employee ID="2">
<FirstName>Peter</FirstName>
<LastName>Pan</LastName>
<PhoneNumber>604-111-1111</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<JobTitle>Sr. Developer</JobTitle>
</Employee>
</Employees>
</Company>
 
G

Guest

You need to add the namespaces to the XmlDocument object. You can find sample
code using NameSpaceManager in overloaded constructors in the documentation
for the XmlDocument class.
Pete
 
J

Jason Zhou

Thanks Pete, I will take a look.

Jason

Peter Bromberg said:
You need to add the namespaces to the XmlDocument object. You can find
sample
code using NameSpaceManager 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 said:
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>Klaus</FirstName>
<LastName>Salchner</LastName>
<PhoneNumber>410-727-5112</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<WebAddress>http://www.enterprise-minds.com</WebAddress>
<JobTitle>Sr. Enterprise Architect</JobTitle>
</Employee>
<Employee ID="2">
<FirstName>Peter</FirstName>
<LastName>Pan</LastName>
<PhoneNumber>604-111-1111</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<JobTitle>Sr. Developer</JobTitle>
</Employee>
</Employees>
</Company>

I can use following command to search the node:
XmlNode oNode1 =
oXML.SelectSingleNode("/descendant::JobTitle[ancestor::Employee/@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>Klaus</FirstName>
<LastName>Salchner</LastName>
<PhoneNumber>410-727-5112</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<WebAddress>http://www.enterprise-minds.com</WebAddress>
<JobTitle>Sr. Enterprise Architect</JobTitle>
</Employee>
<Employee ID="2">
<FirstName>Peter</FirstName>
<LastName>Pan</LastName>
<PhoneNumber>604-111-1111</PhoneNumber>
<EmailAddress>[email protected]</EmailAddress>
<JobTitle>Sr. Developer</JobTitle>
</Employee>
</Employees>
</Company>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top