1

I have a complex structure xml from which I need to read some element value. My xml is somewhat like this:

<plist version="1.0">
<dict>
<key>XYZ</key>
<dict>
    <key>KEYVALUE1</key>
    <dict>
        <key>A</key>
        <date>AVALUE1</date>
        <key>B</key>
        <string>BVALUE1</string>            
    </dict>
    <key>KEYVALUE2</key>
    <dict>
        <key>A</key>
        <date>AVALUE2</date>
        <key>B</key>
        <string>BVALUE2</string>    
        <key>C</key>
        <string>CVALUE2</string>        
    </dict>
</dict>
</dict>
</plist>

What I need is: search for a dict with KEYVALUE2, and wherever I get it, pick BVALUE2 out of it (you can replace 2 with whatever number, I have just included 2 nodes for brevity).

I am newbie for xml programming and all my attempts to try out MS documentation have only confused me more. Sometimes I find a xmlreader example which didn't quite serve my purpose, and other time I got LINQ example which confused me because of its structure. Please help!

2
  • 3
    Any chance you could improve your XML structure by having key/value pair elements? Relying on the interleaving of keys and values is relatively nasty... Commented Aug 3, 2012 at 9:36
  • what do you mean with "dict with KEYVALUE2"? The dict with KEYVALUE2 in your example is the same as the dict with KEYVALUE1. There is one dict that has 2 keys (and some other elements) that are KEYVALUE1 and KEYVALUE2. Commented Aug 3, 2012 at 9:36

1 Answer 1

3

There are many technologies that allow you to access XML.
Since linq wasn't your taste, you could do the following:
Use XmlDocument with XPath, such as in this example:

XmlDocument xmldoc = new XmlDocument();
xmlDoc.LoadXml(xml);
string bvalue = xmlDoc.SelectSingleNode("//dict[key='" + key + "']/dict/string).InnerText;

But I would advice changing the structure of your XML first..
XML is a great relational data structure. The way your keys are arranged is not relational.. In my opinion, there should be one key on each level like this:

<dict>     
    <KeyValuePair>
        <key type="date">A</key>        
        <value>AVALUE2</value>
    </KeyValuePair>
    <KeyValuePair>
        <key type="string">B</key>        
        <value>BVALUE2</value>            
    </KeyValuePair>        
</dict>  

That way you could write:

string value = xmlDoc.SelectSingleNode("//KeyValuePair[key = 'B']/value").InnerText;

Good Luck!

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

5 Comments

Many Thanks! However this solution doesn't serve my purpose. Firstly, as a matter of fact, xml structure is set in stone, I have no way to change it. Secondly, I need BVALUE2 because it is a value for the key named 'B'. In context of my sample xml, I supplied "//dict[key='XYZ']//dict[key='B']//string" to your 3rd line, but it doesn't give me BVALUE2. Instead, it gives me some CVALUE2 (where type of CVALUE2 is also string, and it appears before BVALUE2 in the document, on the same level as BVALUE2).
@Nirav, your query returns multiple results, and SelectSingleNode will return the first one that matches. The problem is the structure of the XML won't allow you to come up with an easy query.. You could write this query: //key[text()='KEYVALUE2']/following::node()/key[text()='B']/following::node()/text() but as you see it's not that great
You could also iterate the nodes using SelectNodes method instead.
OK, I edited my original post to include CVALUE2 which I really want. But instead I get BVALUE2. [also note that there is no CVALUE1 in the structure, I may just want CVALUE2, because it all depends on whether I encounter key=C or not.
You can download some free xpath editor like this and play a little with your xml (double-click on the top bar to get the xpath editor). If you place C instead of B in the sample query I gave you, you would get the value you want. But with this kind of structure I don't know if you can count on the following:: function...

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.