I have simple XML which I would like to query for mnemonic collection.
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<logs version="1.3.1.1" xmlns="开发者_如何学运维http://www.witsml.org/schemas/131">
<log>
<startIndex uom="m">200.29</startIndex>
<endIndex uom="m">209.73</endIndex>
<logCurveInfo>
<mnemonic>DEPTH</mnemonic>
</logCurveInfo><logCurveInfo>
<mnemonic>VDEPTH</mnemonic>
</logCurveInfo>
<logCurveInfo>
<mnemonic>ropAv1</mnemonic>
</logCurveInfo>
<logCurveInfo>
<mnemonic>wobAv1</mnemonic>
</logCurveInfo>
<logCurveInfo>
<mnemonic>hkldAv1</mnemonic>
</logCurveInfo>
<logData>
<data />
</logData>
</log>
</logs>
I have tried with,
XDocument xDoc = XDocument.Load(@"e:\sampleXml.xml");
var q = from c in xDoc.Descendants("logCurveInfo")
select c.Element("mnemonic").Value;
foreach (string item in q)
{
MessageBox.Show(item);
}
Though the query is getting executed, but I am not getting anything as output. I expect each mnemonic to be shown on the messagebox in the loop.
The problem lies with how namespaces are resolved. You need to change you code to something like:
XDocument doc = XDocument.Load(@"e:\sampleXml.xml");
XNamespace xmlns = "http://www.witsml.org/schemas/131";
var q = from c in doc.Descendants(xmlns+"logCurveInfo")
select c.Element(xmlns+"mnemonic").Value;
foreach (string item in q)
{
MessageBox.Show(item);
}
精彩评论