0

大家好,我尝试使用我拥有的属性值过滤 xml 文件并获取该节点的所有其他属性值。

这是我的 C# 代码:

string search_string = "GlobalZoneEU";

            XElement xelement = XElement.Load(path_of_hw);
            var node = from nm in xelement.Elements("LICENSE_PATH")
                       where (string)nm.Element("FEATURE").Attribute("NAME") == search_string
                       select nm;

            Console.WriteLine("Show");

            foreach (XElement xle in node)
            {
                Console.WriteLine(xle.Attribute("NAME").Value);
                Console.WriteLine(xle.Attribute("VERSION").Value);
                Console.WriteLine(xle.Attribute("VENDOR").Value);
                Console.WriteLine(xle.Attribute("START").Value);
                Console.WriteLine(xle.Attribute("END").Value);
                Console.WriteLine(xle.Attribute("USED_LICENSES").Value);
                Console.WriteLine(xle.Attribute("TOTAL_LICENSES").Value);
            }

            Console.ReadLine(); 

这是我的测试 xml 的片段:

<?xml version="1.0" encoding="UTF-8"?>
<LM-X STAT_VERSION="3.32">
<LICENSE_PATH TYPE="NETWORK" HOST="6200@lwserv005" SERVER_VERSION="4.4.4" UPTIME="53 day(s) 21 hour(s) 10 min(s) 50 sec(s)">
<FEATURE NAME="GlobalZoneEU" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="111720" TOTAL_LICENSES="147000" SHARE="CUSTOM ,VIRTUAL">
<USER NAME="SYSTEM" HOST="LWSERV171" IP="172.16.11.115" USED_LICENSES="2000" LOGIN_TIME="2013-04-17 12:42" CHECKOUT_TIME="2013-04-17 12:42" SHARE_CUSTOM="hweuser:172.16.11.115"/>
>
<USER NAME="admin" HOST="SERV171" IP="172.166.11.115" USED_LICENSES="720" LOGIN_TIME="2013-04-17 12:44" CHECKOUT_TIME="2013-04-17 12:44" SHARE_CUSTOM="pbsadmin:LWSERV171:1592_40960072_1356792762_826820"/>
>
<USER NAME="Be.M" HOST="4327" IP="172.168.225.105" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 07:52" CHECKOUT_TIME="2013-05-21 07:52" SHARE_CUSTOM="Be.M:4327"/>
>
<USER NAME="Bell.M" HOST="DBG4327" IP="172.16.225.105" USED_LICENSES="6000" LOGIN_TIME="2013-05-21 07:54" CHECKOUT_TIME="2013-05-21 07:54" SHARE_CUSTOM="Bell.M:DBG4327"/>
>
<USER NAME="xu.d" HOST="DBG4970" IP="172.16.225.126" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:15" CHECKOUT_TIME="2013-05-21 08:15" SHARE_CUSTOM="xu.d:DBG4970"/>
>
<USER NAME="xu.d" HOST="DBG4970" IP="172.16.225.126" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:18" CHECKOUT_TIME="2013-05-21 08:18" SHARE_CUSTOM="xu.d:DBG4970"/>
>
<USER NAME="Bell.M" HOST="DBG4327" IP="172.16.225.105" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:23" CHECKOUT_TIME="2013-05-21 08:23" SHARE_CUSTOM="Bell.M:DBG4327"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:36" CHECKOUT_TIME="2013-05-21 08:36" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="6000" LOGIN_TIME="2013-05-21 08:37" CHECKOUT_TIME="2013-05-21 08:37" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="hassan.a" HOST="DBG5199" IP="172.16.225.114" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 09:24" CHECKOUT_TIME="2013-05-21 09:24" SHARE_CUSTOM="hassan.a:DBG5199"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 09:33" CHECKOUT_TIME="2013-05-21 09:33" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="6000" LOGIN_TIME="2013-05-21 09:33" CHECKOUT_TIME="2013-05-21 09:33" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="xu.d" HOST="SIM02" IP="172.16.32.12" USED_LICENSES="2000" LOGIN_TIME="2013-05-21 09:39" CHECKOUT_TIME="2013-05-21 09:39" SHARE_CUSTOM="xu.d:SIM02"/>
>
<USER NAME="zeiser-rasumak.a" HOST="DBG5200" IP="172.16.225.127" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 10:05" CHECKOUT_TIME="2013-05-21 10:05" SHARE_CUSTOM="zeiser-rasumak.a:DBG5200"/>
>
<USER NAME="xu.d" HOST="DBG4970" IP="172.16.225.126" USED_LICENSES="25000" LOGIN_TIME="2013-05-21 10:25" CHECKOUT_TIME="2013-05-21 10:25" SHARE_CUSTOM="xu.d:DBG4970"/>
>
<USER NAME="zeiser-rasumak.a" HOST="DBG5200" IP="172.16.225.127" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 11:27" CHECKOUT_TIME="2013-05-21 11:27" SHARE_CUSTOM="zeiser-rasumak.a:DBG5200"/>
>
<USER NAME="zeiser-rasumak.a" HOST="DBG5200" IP="172.16.225.127" USED_LICENSES="2000" LOGIN_TIME="2013-05-21 11:48" CHECKOUT_TIME="2013-05-21 11:48" SHARE_CUSTOM="zeiser-rasumak.a:DBG5200"/>
>
<USER NAME="zeiser-rasumak.a" HOST="DBG5200" IP="172.16.225.127" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 11:49" CHECKOUT_TIME="2013-05-21 11:49" SHARE_CUSTOM="zeiser-rasumak.a:DBG5200"/>
</FEATURE>
<FEATURE NAME="HWAIFPBS" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="0" TOTAL_LICENSES="2147483647" SHARE="CUSTOM ,VIRTUAL"/>
<FEATURE NAME="HWAWPF" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="0" TOTAL_LICENSES="2147483647" SHARE="CUSTOM ,VIRTUAL"/>

我想获取其他属性值...

4

1 回答 1

1

你可以使用Parent属性。我已经修改了您的 xml 以使其有效。下一个代码打印 foreach 循环中的所有值

string search_string = "GlobalZoneEU";

var xDoc = XDocument.Load(path_of_hw);

var users = xDoc.Root
                .Elements("LICENSE_PATH")
                .Select(license => license.Element("FEATURE"))
                .Where(feature => (string)feature.Attribute("NAME") == search_string)
                .Select(feature => feature.Element("USER"));

foreach (var xle in users)
{
    Console.WriteLine(xle.Attribute("NAME").Value);
    Console.WriteLine(xle.Parent.Attribute("VERSION").Value);
    Console.WriteLine(xle.Parent.Attribute("VENDOR").Value);
    Console.WriteLine(xle.Parent.Attribute("START").Value);
    Console.WriteLine(xle.Parent.Attribute("END").Value);
    Console.WriteLine(xle.Attribute("USED_LICENSES").Value);
    Console.WriteLine(xle.Parent.Attribute("TOTAL_LICENSES").Value);
}

印刷:

SYSTEM
12.0
ALTAIR
2013-03-26
2014-03-31
2000
147000
于 2013-05-23T08:53:53.350 回答