Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# extract value from nextnode

Tags:

c#

xml

I have the following xml part and am trying to extract the value where key is known. The example below is a snippet, from a larger xml that contains 1000's of nodes.

<?xml version="1.0" encoding="utf-8"?>
<DictionarySerializer>  
    <item>
        <key>key1</key>
        <value>CONTENT1</value>
    </item>
    <item>
        <key>key2</key>
        <value>CONTENT2</value>
    </item>
</DictionarySerializer>

i assume the above is a string called xml, then with

XDocument.Parse(xml)
.Descendants("key")
.Where(x => (string)x.Value == "key1")
.FirstOrDefault().NextNode.ToString()

I can get the string <value>CONTENT1</value> But i simply cannot get my head around how to get the value of the value node to to say. I am afrad it is super simple, and i just are stuck in a coffein loop :-)

like image 241
Kim Isaksen Avatar asked Nov 28 '25 11:11

Kim Isaksen


2 Answers

XDocument.Parse(xml)
.Descendants("key")
.Where(x => (string)x.Value == "key1")
.FirstOrDefault().Value.ToString()

you should use .Value property instead of .NextNode

like image 121
soumya sambit Kunda Avatar answered Nov 29 '25 23:11

soumya sambit Kunda


Try to cast NextNode to XElement and get Value from it.

like image 44
ideafixxxer Avatar answered Nov 29 '25 23:11

ideafixxxer