我有如下所示的XML。我正在嘗試使用LINQ解析C#。Linq查詢解析XML
<software>
<version>31.0.1</version>
<status>uptime 2d 22h 39m 26s</status>
<wPack>
<rv>
<total>0</total>
<qv>0</qv>
</rv>
<sv>
<total>0</total>
<qv>0</qv>
</sv>
</wPack>
<sPack>
<rv>
<total>242</total>
<qv>1</qv>
</rv>
<sv>
<total>69845</total>
<qv>145</qv>
</sv>
<size>146</size>
</sPack>
<dPack>
<rv>
<total>88560</total>
</rv>
<sv>
<total>0</total>
</sv>
<in>0.28,0.23,0.35</in>
<out>0.00,0.00,0.00</out>
<qv>216806</qv>
<db>mysql</db>
</dPack>
<bClients>
<bClient>
<type>sPackbClient</type>
<id>test1</id>
<IP>127.0.0.1</IP>
<queue>0</queue>
<status>on-line 2d 22h 39m 21s</status>
<ssl>no</ssl>
</bClient>
<bClient>
<type>sPackbClient</type>
<id>test2</id>
<IP>127.0.0.1</IP>
<queue>0</queue>
<status>on-line 2d 22h 39m 18s</status>
<ssl>no</ssl>
</bClient>
<bClient>
<type>sPackbClient</type>
<id>test3</id>
<IP>127.0.0.1</IP>
<queue>0</queue>
<status>on-line 0d 2h 33m 30s</status>
<ssl>no</ssl>
</bClient>
</bClients>
<servers>
<server>
<name>EC1</name>
<admin-id>EC1</admin-id>
<id>EC1</id>
<status>online 8901s</status>
<failed>0</failed>
<qv>0</qv>
<sPack>
<rv>0</rv>
<sv>0</sv>
<in>0.00,0.00,0.00</in>
<out>0.00,0.00,0.00</out>
</sPack>
<dPack>
<rv>0</rv>
<sv>0</sv>
<in>0.00,0.00,0.00</in>
<out>0.00,0.00,0.00</out>
</dPack>
</server>
<server>
<name>EC2</name>
<admin-id>EC2</admin-id>
<id>EC2</id>
<status>online 8918s</status>
<failed>2</failed>
<qv>0</qv>
<sPack>
<rv>79</rv>
<sv>20843</sv>
<in>0.00,0.00,0.00</in>
<out>0.06,0.05,0.08</out>
</sPack>
<dPack>
<rv>35050</rv>
<sv>0</sv>
<in>0.10,0.07,0.14</in>
<out>0.00,0.00,0.00</out>
</dPack>
</server>
<server>
<name>EC3</name>
<admin-id>EC3</admin-id>
<id>EC3</id>
<status>re-connecting</status>
<failed>0</failed>
<qv>0</qv>
<sPack>
<rv>4</rv>
<sv>1671</sv>
<in>0.00,0.00,0.00</in>
<out>0.00,0.00,0.00</out>
</sPack>
<dPack>
<rv>1664</rv>
<sv>0</sv>
<in>0.00,0.00,0.00</in>
<out>0.00,0.00,0.00</out>
</dPack>
</server>
</servers>
</software>
當我嘗試使用以下查詢獲取<sPack>
元素。
var software = (from sw in xDoc.Descendants("software")
from sp in sw.Descendants("sPack")
select sp).ToList();
我得到它正在的<sPack>
所有的實例:
<servers>
<server>
<sPack>
我要的是讓<sPack>
其自帶<software>
和解析<servers>
一個單獨的查詢下。
你可能會考慮XPath。它和selectNodes一樣簡單(「/ servers/servers/server/sPack」) –