2

I have a set of xml that i have in an XElement that looks like this:

<Object type="Item_Element">
  <Property name="IDValue1" value="somevaluethatihave"/>
  <Property name="IDValue2" value="somevaluethatineed"/>
  <Property name="IDValue3" value="somevaluethatihaveanddonotneed"/>
</Object>

I would like to get the value attribute value of IDValue2 as a string instead of as an XElement

I have tried doing so with this:

var meID = from el in linkedTeethInfo.DescendantsAndSelf("Property") 
    where (string)el.Attribute("name") == "IDValue2" 
    select el.Attribute("value");

As well as some other combinations that did not work and kept returning it in the XElement format listed as an index value. I was wondering if it would be possible to get the single value somevaluethatineed as a string? I would preferably like to have this using one variable and not have to break this down into multiple steps.

2
  • 1
    XElement.Attribute returns an XAttribute: Are you sure you're getting an XElement because that's not what the quoted code will return (IEnumerable<XAttribute>). Have you considered getting the Value property of each returned node? Commented Dec 10, 2014 at 13:27
  • @Richard - It may be an XAttribute. Like I said, I am still trying to get my barrings with all of this. I have also tried getting the Value and it leaves me in the same position. Commented Dec 10, 2014 at 13:56

2 Answers 2

2

XElement class provides Value property. You can use it to get the text associated with the element:

IEnumerable<string> meID = from el in linkedTeethInfo.DescendantsAndSelf("Property") 
    where (string)el.Attribute("name") == "IDValue2" 
    select el.Attribute("value").Value;

You could also cast your attribute to string the way you did in the where clause:

IEnumerable<string> meID = from el in linkedTeethInfo.DescendantsAndSelf("Property") 
    where (string)el.Attribute("name") == "IDValue2" 
    select (string)el.Attribute("value");

If you know that there is only one "IDValue2" among the elements, you can get a single string like this:

string meID = (from el in linkedTeethInfo.DescendantsAndSelf("Property") 
    where (string)el.Attribute("name") == "IDValue2" 
    select el.Attribute("value").Value).FirstOrDefault();
Sign up to request clarification or add additional context in comments.

1 Comment

Doing this still provides me with the same type of format that I am currently getting with my variable. Where the value is set to an index. Am I thinking about this the wrong way?
0

You can get that value even without explicit LINQ query (and it looks more elegant for me) like this:

var value = your_XElement
    .XPathSelectElement("Property[@name='IDValue2']")
    .Attribute("value")
    .Value;

3 Comments

.XPathSelectElement is not even an option in intellisense.
@scapegoat17 Have you System.Xml.Linq referenced in your project? See msdn.microsoft.com/en-us/library/bb156083%28v=vs.100%29.aspx for information about XPathSelectElement
@scapegoat17 To access that method you need to have a using System.Xml.XPath in scope as it is an extension method from that namespace.

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.