0

I get a XML - File like this:

<list>
<sublist id1="a" id2="b" id3="b" id4="b">
    <item name="name1">
        <property1>a</property1>
     </item>
    <item2 name="name2">
        <property1>c</property1>
    </item2>
</sublist>
<sublist id1="b" id2="b" id3="a" id4="b">        
 [...more XML here...]
</sublist>

How can I find out the attribute name of the element "sublist" with the value "a" ?

4
  • which attribute, which value ? Commented Aug 13, 2014 at 16:41
  • 1
    OP is looking to find id1, based on the fact that that is the name of the attribute that has value a. Commented Aug 13, 2014 at 16:42
  • Should be a fairly simple Linq2XML query. Commented Aug 13, 2014 at 16:45
  • Where's the code you have so far? There is no shortage of XML libraries, and the answer's going to be tied to the one that you've chosen and are using. Commented Aug 13, 2014 at 16:47

1 Answer 1

4

If you have the XML string in a variable called xml:

XDocument
  .Parse(xml)                      // parse the XML
  .Descendants()                   // get all descendant elements
  .SelectMany(e => e.Attributes()) // get all attributes from all elements
  .Where(a => a.Value == "a")      // leave only attributes whose value is "a"
  .Select(a => a.Name);            // select the name of those attributes

The result:

id1
id3

Note that this code uses XElement and LINQ to accomplish the goal. There are many other ways, some of which may be better suited to your needs.

Update

I noticed now that you're only looking for attributes on sublist elements. The code can be modified to handle that:

XDocument
  .Parse(xml)                      // parse the XML
  .Descendants("sublist")          // get all descendant elements named "sublist"
  .SelectMany(e => e.Attributes()) // get all attributes from all elements
  .Where(a => a.Value == "a")      // leave only attributes whose value is "a"
  .Select(a => a.Name);            // select the name of those attributes

The difference is in the call to Descendants, which now filters out any elements that is not called sublist.

In a comment you also asked how to handle the case when there is just a single sublist element. The code snippets above work fine for that too, since they're not making any assumptions about the number of elements.

You might be tempted to handle that case differently, such as with this code:

XDocument
  .Parse(xml)                      // parse the XML
  .Descendants("sublist")          // get all descendant elements named sublist
  .Single()                        // get the one and only element
  .Attributes()                    // get all attributes from all elements
  .Where(a => a.Value == "a")      // leave only attributes whose value is "a"
  .Select(a => a.Name)             // select the name of those attributes

The difference between this and the previous examples is that we use Single here to extract the one and only sublist element from the result. The item type in the code at that point becomes XElement, and Single will throw an exception if there are no ("Sequence contains no elements") or more than one ("Sequence contains more than one element") sublist elements. In the next line we can then get rid of the SelectMany call and just access the Attributes of the XElement straight away.

But in my opinion the change in the code is not worth the loss in robustness you'll have.

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

3 Comments

First of all thanks for your help @Frank: How can I convert the answer of your query in case that I only have the element "sublist" once in my XML- file ?
@ Frank the answer you gave me is nearly the solution (Thanks for your effort) - one additional question: how can I make a string out of that ?
Note sure what you mean (it would really help if you also share some code you wrote). But any object in .NET can be converted to a string by calling its ToString() method.

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.