?
Solved

Simple 'get elements by name' XPath question (in C#)

Posted on 2007-10-18
3
Medium Priority
?
998 Views
Last Modified: 2012-05-05
Perphaps someone can explain to me why I do not find what I expect with XPath in an XmlDocument. I have an XmlNode (called xml). If I run xml.OuterXml I get the following:

<MirrorAttributeChangeResponse xmlns="company/ws/shared/attribute">
  <MirrorAttributeChangeResult>
    <AttributeChange>
      <FieldName>coloredfield</FieldName>
      <AttributeName>style</AttributeName>
      <AttributeValue>;background-color: red</AttributeValue>
      <Overwrite>false</Overwrite>
    </AttributeChange>
    <AttributeChange>
      <FieldName>coloredfield</FieldName>
      <AttributeName>style</AttributeName>
      <AttributeValue>;background-color: red</AttributeValue>
      <Overwrite>false</Overwrite>
    </AttributeChange>
    <AttributeChange>
      <FieldName>coloredfield</FieldName>
      <AttributeName>style</AttributeName>
      <AttributeValue>;background-color: red</AttributeValue>
      <Overwrite>false</Overwrite>
    </AttributeChange>
  </MirrorAttributeChangeResult>
</MirrorAttributeChangeResponse>

If I run xml.SelectNodes("//AttributeChange") I expect to get the 3 AttributeChange nodes, but I get 0 nodes. Why? Does it have something to do with namespaces?
0
Comment
Question by:gyxi
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 20099970
yes, the namespace prevents the XPath from seeing the three nodes
you don't need AttributeChange, but  {company/ws/shared/attribute}:AttributeChange (that is a pseudo notation)

there are two options
- you add a namespacemanager to the query
- or you go for the localname()

xml.SelectNodes("//*[local-name() = 'AttributeChange']")
will give you what you need
0
 

Author Comment

by:gyxi
ID: 20100815
Thanks Gertone, that solved it. I hope it still performs about the same. It looks kind of expensive.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20101238
welcomr,

it IS expensive, hopefully your XML is not too big
If it is too expensive in your program, use a namespace manager
http://www.timrayburn.net/default,date,2005-01-15.aspx
(scroll down the page to actually get the namespace black belt article of 15-jan
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question