0

My XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <something.logger>
    <add key="LoggerId" value="8a2ff9ef-d144-4dcb-86d8-6ccaf44def20">
    </add>
    <add key="FederationId" value="00000000-0000-0000-0000-000000000000" />
  </something.logger>
 </configuration>

My code:

XmlDocument xml = new XmlDocument();
xml.Load(Some-Valid-Path);
XmlNodeList xnList = xml.SelectNodes("/configuration/something.logger");

I am trying to get the Guid (or value..) of value.

In the end i want to get the string "8a2ff9ef-d144-4dcb-86d8-6ccaf44def20"

Thanks !

ilansch
  • 4,484
  • 5
  • 40
  • 83
  • 1
    possible duplicate of [How to extract attribute’s value through XPath](http://stackoverflow.com/questions/4835891/how-to-extract-attribute-s-value-through-xpath) – nvoigt Aug 05 '14 at 14:14

2 Answers2

1

Use /@ to access attributes:

XmlNodeList xnList = xml.SelectNodes("/configuration/something.logger/add/@value");
Kabulan0lak
  • 2,021
  • 1
  • 15
  • 27
1

Use @ at the beginning of attribute name to reference an attribute in XPath. Then you need to cast each item in list as XmlAttribute :

XmlNodeList xnList = doc.SelectNodes("/configuration/something.logger/add[@key='LoggerId']/@value");
foreach (XmlAttribute n in xnList)
{
    Console.WriteLine(n.Value);
}
har07
  • 83,990
  • 12
  • 70
  • 116
  • What condition should i add in order to retrieve only LoggerId value ? Sorry for being noobish - I Mean where key=LoggerId -> i need only that string. the n returned in your sample is only value - since thats what written in "configuration/something.logger/add/@value" – ilansch Aug 05 '14 at 14:28
  • Edited my answer to accommodate that – har07 Aug 05 '14 at 14:37
  • Hi man thanks alot i just seen, i took your sample and then started to create XmlElement from each Selected Node, and then browsing on each XmlElement attributes to find the LoggerId, your solution is faster. using the xpath with addition of [@key='LoggerId'] – ilansch Aug 05 '14 at 14:38