Reputation: 5628
<plist version="1.0">
<dict>
<key>DataType</key>
<string>Mandant</string>
<key>Version</key>
<string>1.5</string>
<key>MandantMandantA</key>
<dict>
<key>MandantBezeichnung</key>
<string>MandantA</string>
</dict>
<key>MandantMandantB</key>
<dict>
<key>MandantBezeichnung</key>
<string>MandantB</string>
</dict>
</dict>
</plist>
How will i read the value inside the second key element ?
I need a result of 1.5
I was trying around, but wasn't successfull yet
XElement doc = XElement.Parse(xml);
//var nodes = doc.DescendantNodes();
//var nodes2 = doc.Nodes().;
//string keyValue = ((XElement)doc.Descendants().Descendants("dict").ElementAt(1).NodesBeforeSelf().Last()).Value;
int countElements = doc.Descendants().Count();
for (int i = 0; i < countElements; i++)
{
string test = doc.Element("key").Value;
//string keyValue = ((XElement)doc.Nodes().ElementAt(i).Last()).Value;
}
//string keyValue = ((XElement)doc.Descendants("dict/Version").DescendantNodes().Last().
Upvotes: 0
Views: 165
Reputation: 7556
try linq to xml
string xml = @"<root>
<fruit>1</fruit>
<tree>2</tree>
</root>";
XDocument dox = XDocument.Parse(xml);
var node = dox.Descendants().Where(n => n.Name == "fruit").FirstOrDefault();
string nodeValue;
if (node != null)
nodeValue = node.Value;
In your case
doc.Descendants("key").Where(x => x.Name == "version").NodesAfterSelf().OfType<XElement>().First();
Upvotes: 0
Reputation: 5402
Assuming your XML schema is fixed and you don't need to check against erroneous xml:
var result = (doc.Descendants("key")
.Single(e => e.Value == "Version")
.NextNode as XElement).Value;
Upvotes: 2