0

'm having a problem figuring out the correct select statement here

I have the following XML

<configuration>
  <other sections>
  <runtime>
    <Binding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing Path="some path string" />
    </Binding>
    <Concurrent enabled="false" />
  </runtime>
  <other sections>
</configuration>

Im trying to do a select where I retrieve the Path string value

So far I have this

XDocument xdoc = XDocument.Load(XmlfilePath);

var query = (from c in xdoc.Descendants("probing")
where c.Attribute("Path") != null
select c.Attribute("Path").Value).FirstOrDefault();

But this doesn't work, query is null

1 Answer 1

3

Because the name of your attribute is Path not privatePath.Also you can use explicit cast and then you don't need the null-check:

var query = (from c in xdoc.Descendants("probing")
             select (string)c.Attribute("Path")).FirstOrDefault();

Update: it seems your element has a namespace so you need to specify the namespace like this:

XNamespace ns = "urn:schemas-microsoft-com:asm.v1";

var query = (from c in xdoc.Descendants(ns + "probing")
             select (string)c.Attribute("Path")).FirstOrDefault();

You may want to take a look at the documentation for more details about xml namespaces: Working with XML Namespaces

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, for the example I made "privatePath" in the xml just Path, forgot to change the code sample. I tried your example just now, but its still null the query value
@Domitius now I see the namespace sorry.I updated my answer try it again.

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.