For example you want to extract from XML Key - Value - Pair. First "value" node is key, second "value" node is value.
Example XML:
<params>
<param>
<value>
<array>
<data>
<value>
<string>NameOfValueOne</string>
</value>
<value>
<string>ValueOne</string>
</value>
</data>
</array>
</value>
</param>
<param>
<value>
<array>
<data>
<value>
<string>NameOfValueTwo</string>
</value>
<value>
<string>ValueTwo</string>
</value>
</data>
</array>
</value>
</param>
</params>
Code:
Dictionary<string, string> myDict = new Dictionary<string, string>();
XElement myXML = XElement.Load("xml.xml");
IEnumerable<XElement> xElementData = from data in myXML.Descendants("data") select data;
foreach (XElement xElement in xElementData)
{
myDict[(string)xElement.Elements().ElementAt(0)] = (string)xElement.Elements().ElementAt(1);
}
foreach (KeyValuePair<string, string> keyValuePair in myDict)
{
Console.WriteLine($"Key: {keyValuePair.Key}, value: {keyValuePair.Value}");
}
Console.WriteLine("Press any key");
Console.ReadKey();
POI:
IEnumerable<XElement> xElementData = from data in myXML.Descendants("data") select data;
foreach (XElement xElement in xElementData)
{
myDict[(string)xElement.Elements().ElementAt(0)] = (string)xElement.Elements().ElementAt(1);
}
Source download from
here.