如何使用 LINQ to XML 从具有相同元素和属性的复杂 XML 中提取数据

本文关键字:XML 属性 复杂 元素 提取 数据 to LINQ 何使用 | 更新日期: 2023-09-27 17:56:26

这是我第一次使用 Linq 到 xml,我正在努力从 xml 文件中提取一些数据。问题似乎是由于XML的格式化方式(这是我无法控制的),因为它具有相同的修饰和属性。

<host starttime="1357755777" endtime="1357755993">
    <status state="up" reason="arp-response"/>
    <address addr="192.168.1.1" addrtype="ipv4"/>
    <address addr="00:50:56:90:77:9F" addrtype="mac" vendor="VMware"/>
    <hostnames>
        <hostname name="test1.test.com" type="PTR"/>
    </hostnames>
    <ports>
        <extraports state="closed" count="95">
            <extrareasons reason="resets" count="95"/>
        </extraports>
        <port protocol="tcp" portid="135">
            <state state="open" reason="syn-ack" reason_ttl="128"/>
            <service name="msrpc" product="Microsoft Windows RPC" ostype="Windows" method="probed" conf="10">
                <cpe>cpe:/o:microsoft:windows</cpe>
            </service>
        </port>
        <port protocol="tcp" portid="139">
            <state state="open" reason="syn-ack" reason_ttl="128"/>
            <service name="netbios-ssn" method="probed" conf="10"/>
        </port>
        <port protocol="tcp" portid="445">
            <state state="open" reason="syn-ack" reason_ttl="128"/>
            <service name="microsoft-ds" product="Microsoft Windows 2003 or 2008 microsoft-ds" ostype="Windows" method="probed" conf="10">
                <cpe>cpe:/o:microsoft:windows</cpe>
            </service>
        </port>
        <port protocol="tcp" portid="3389">
            <state state="open" reason="syn-ack" reason_ttl="128"/>
            <service name="ms-wbt-server" product="Microsoft Terminal Service" ostype="Windows" method="probed" conf="10"/>
        </port>
        <port protocol="tcp" portid="8081">
            <state state="open" reason="syn-ack" reason_ttl="128"/>
            <service name="http" product="Network Associates ePolicy Orchestrator" method="probed" conf="10"/>
        </port>
    </ports>
</host>
<host starttime="1357755777" endtime="1357755993">
    <status state="up" reason="arp-response"/>
    <address addr="192.168.1.2" addrtype="ipv4"/>
    <address addr="00:50:56:90:67:8F" addrtype="mac" vendor="VMware"/>
    <hostnames>
        <hostname name="test2.test.com" type="PTR"/>
    </hostnames>
    <ports>
        <extraports state="closed" count="97">
            <extrareasons reason="resets" count="97"/>
        </extraports>
        <port protocol="tcp" portid="53">
            <state state="open" reason="syn-ack" reason_ttl="64"/>
            <service name="domain" product="dnsmasq" version="2.33" method="probed" conf="10">
                <cpe>cpe:/a:thekelleys:dnsmasq:2.33</cpe>
            </service>
            <script id="dns-nsid" output="&#xa;  bind.version: dnsmasq-2.33&#xa;"/>
        </port>
        <port protocol="tcp" portid="81">
            <state state="open" reason="syn-ack" reason_ttl="64"/>
            <service name="http" product="Apache httpd" method="probed" conf="10">
                <cpe>cpe:/a:apache:http_server</cpe>
            </service>
            <script id="http-title" output="Did not follow redirect to https://192.168.100.14:445/ and no page was returned."/>
            <script id="http-favicon" output="Unknown favicon MD5: 95CDE3E49C5B2645F99AAAAABB6CD4C6"/>
            <script id="http-methods" output="No Allow or Public header in OPTIONS response (status code 403)"/>
        </port>
        <port protocol="tcp" portid="445">
            <state state="open" reason="syn-ack" reason_ttl="64"/>
            <service name="http" product="Apache httpd" method="probed" conf="10">
                <cpe>cpe:/a:apache:http_server</cpe>
            </service>
            <script id="http-title" output="400 Bad Request"/>
            <script id="http-methods" output="No Allow or Public header in OPTIONS response (status code 403)"/>
        </port>
    </ports>
</host>

以上是我必须使用的 XML 示例对于这个问题,我稍微简化了一下,输出来自nmap。

我需要从 XML 获得的数据如下。对于每个主机状态/状态地址/地址,用于地址类型 IPv4地址/地址和地址/供应商 地址/供应商 用于 addrtype MAC每个端口/端口的

    XDocument NmapScan = XDocument.Load(file);
    var data = from item in NmapScan.Descendants("host")
         select new 
         {
             status = item.Element("status").Attribute("state").Value,
             ip = item.Element("address").Attribute("addr").Value,
             iptype = item.Element("address").Attribute("addrtype").Value
         };
                foreach (var p in data)
                    Debug.WriteLine(p.ToString());

我找到的每个教程都没有看到这种类型的XML进入这个。我可以得到每种类型的第一个条目,但不能获得第二个条目。我一直无法找到一种方法来遍历它们中的每一个。我想要的是将其用于输出

状态 = up,IP = 192.168.100.171,iptype = IPv4,端口 = 22,端口 = 80

如何使用 LINQ to XML 从具有相同元素和属性的复杂 XML 中提取数据

您可以在原始选择中进行新选择。请参阅答案中的示例 如何将大型 xml 文件的一部分反序列化为 c# 类?

使用 LINQ:

foreach (XElement hostElement in NmapScan.Descendants("host"))
{
    // Gets the XElement "address" that have the attribute "addrtype" set to "mac"
    XElement macAddressElement = (from addressElement in hostElement.Elements("address")
                                    where addressElement.Attribute("addrtype").Value == "mac"
                                    select addressElement).Single();
    // Gets the XElement "address" that have the attribute "addrtype" set to "ipv4"
    XElement ipV4AdressElement = (from addressElement in hostElement.Elements("address")
                                    where addressElement.Attribute("addrtype").Value == "ipv4"
                                    select addressElement).Single();
    var p = new
    {
        status = hostElement.Element("status").Attribute("state").Value,
        addrIpv4 = ipV4AdressElement.Attribute("addr").Value,
        addrMac = macAddressElement.Attribute("addr").Value,
        addrVendor = macAddressElement.Attribute("vendor").Value,
        ports = (from portElement in hostElement.Element("ports").Elements("port")
                    select portElement.Attribute("portid").Value).ToList()
    };
    Console.WriteLine(p);
}