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 !

1

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

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"
Edited my answer to accommodate that
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']
1

Use /@ to access attributes:

XmlNodeList xnList = xml.SelectNodes("/configuration/something.logger/add/@value");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.