我有这个 XML,有多个“vdsk”类型的元素,我想要它们。为简洁起见,我这里只有 1 个。
<?xml version="1.0" encoding="utf-8" ?>
<diskStatsColl
xmlns="http://ibm.com/storage/management/performance/api/2005/08/vDiskStats"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ibm.com/storage/management/performance/api/2005 /08/vDiskStats schema/SVCPerfStatsV.xsd" scope="node" id="node1" cluster="v7000nd01" node_id="0x0000000000000001" cluster_id="0x00000200a0421666" sizeUnits="512B" timeUnits="msec" contains="virtualDiskStats" timestamp="2013-04-30 07:04:13" timezone="GMT-6:00">
<vdsk idx="0"
ctr="137111307" ctrs="3553815134" ctw="580314768"
ctws="12467258075" ctp="107973069" ctps="6910276416"
ctrh="91819453" ctrhs="2398189867" ctrhp="67411787"/>
<vdsk idx="1"
ctr="137111307" ctrs="3553815134" ctw="580314768"
ctws="12467258075" ctp="107973069" ctps="6910276416"
ctrh="91819453" ctrhs="2398189867" ctrhp="67411787"/>
</diskStatsColl>
我可以获得根元素的属性。我似乎无法获得根的任何子元素。
这段代码有效,但很混乱而且很杂乱
List<XElement> allels = ioxdoc.Elements().ToList();
List<XElement> allelselements = allels[0].Elements().ToList();
var vdisks = from vdisk in allelselements.
Where(a => a.Name.ToString().Contains("vdsk"))
select vdisk;
我试图让它与 XPathSelectElements() 一起工作,我已经根据我在此处找到的示例和示例尝试了这些,但列表始终为空
List<XElement> allels = ioxdoc.Root.XPathSelectElements("vdsk").ToList();
allels = ioxdoc.XPathSelectElements("xml/root/vdsk").ToList();
allels = ioxdoc.XPathSelectElements("/root/vdsk").ToList();