Link to home
Start Free TrialLog in
Avatar of gyxi
gyxi

asked on

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

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?
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gyxi
gyxi

ASKER

Thanks Gertone, that solved it. I hope it still performs about the same. It looks kind of expensive.
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