<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().
有帮助吗?

解决方案

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;

其他提示

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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top