C# 通过属性名获取 Xelement 属性值
C# Get Xelement Attribute Value by Attribute Name
我试图在下面的 xml 代码中获取 operating-system
属性的值,但在我迄今为止从堆栈、dotnetcurry 和 Microsoft 尝试的所有解决方案中,我得到一个 NullPointerExecption
或者只是 return 没有值。
这是我要解析的 xml 数据:
<Report name="Black Workstation" xmlns:cm="http://www.nessus.org/cm">
<ReportHost name="192.168.1.000>
<HostProperties>
<tag name="HOST_END">Wed Jun 29 10:32:54 2016</tag>
<tag name="LastAuthenticatedResults">1467214374</tag>
<tag name="patch-summary-total-cves">5</tag>
<tag name="cpe">cpe:/o:microsoft:windows</tag>
<tag name="operating-system">Microsoft Windows 7 Enterprise Service Pack 1</tag>
</HostProperties>
</ReportHost>
</Report>
我尝试了很多方法,但这里是最后两个:
XNamespace ns = "http://www.nessus.org/cm";
var operatingSystem = (findings.Where(a => a.Attribute("name").Value == "operating-system")
.Select(a => a.Value));
var operatingSystem = findings.Descendants().Where(x => x.Attribute("name").Value == ns + "operating-system");
也试过这个解决方案:Use Linq to Xml with Xml namespaces
我从 Microsoft 教程中获得了此方法,但它只有 returns true/false,我无法对其进行操作以将值 Microsoft Windows 7 Enterprise Service Pack 1
分配给 os
变量。
XElement xelement = XElement.Load(s);
IEnumerable<XElement> findings = xelement.Elements();
var hostProperties = from hp in findings.Descendants("HostProperties")
select new
{
os = (string)hp.Element("tag").Attribute("name").Value == "operating-system",
};
我试过使用其他各种 Linq-to-Xml 查询和上面的 where
子句,但它们都是 return Null
或 no values to enumerate
.
在您的情况下,您不需要应用命名空间:
var result = XDocument.Load("data.xml")
.Descendants("tag")
.Where(e => e.Attribute("name").Value == "operating-system")
.FirstOrDefault()?.Value;
//result = Microsoft Windows 7 Enterprise Service Pack 1
多读一点 here 您实际上可以看到,即使您在 xml none 元素中定义了一个命名空间,您的元素也会使用它。您定义的名称空间 (xmlns:cm
) 仅适用于具有 cm:
前缀的元素,它们不是它们。
请注意,如果我按如下方式更改您的 xml(命名空间仅适用于 xmlns
而不是 xmlns:cm
):
<Report name="Black Workstation" xmlns="http://www.nessus.org/cm">
<ReportHost name="192.168.1.000">
<HostProperties>
<tag name="HOST_END">Wed Jun 29 10:32:54 2016</tag>
<tag name="LastAuthenticatedResults">1467214374</tag>
<tag name="patch-summary-total-cves">5</tag>
<tag name="cpe">cpe:/o:microsoft:windows</tag>
<tag name="operating-system">Microsoft Windows 7 Enterprise Service Pack 1</tag>
</HostProperties>
</ReportHost>
</Report>
上面的代码会returnnull
而你要这样写:
XNamespace ns = "http://www.nessus.org/cm";
var result = XDocument.Load("data.xml")
.Descendants(ns + "tag")
.Where(e => e.Attribute("name").Value == "operating-system")
.FirstOrDefault()?.Value;
请尝试以下操作
XDocument document = XDocument.Load("content.xml");
var name = document.XPathSelectElement("Report/ReportHost/HostProperties/tag[@name='operating-system']").Value;
我试图在下面的 xml 代码中获取 operating-system
属性的值,但在我迄今为止从堆栈、dotnetcurry 和 Microsoft 尝试的所有解决方案中,我得到一个 NullPointerExecption
或者只是 return 没有值。
这是我要解析的 xml 数据:
<Report name="Black Workstation" xmlns:cm="http://www.nessus.org/cm">
<ReportHost name="192.168.1.000>
<HostProperties>
<tag name="HOST_END">Wed Jun 29 10:32:54 2016</tag>
<tag name="LastAuthenticatedResults">1467214374</tag>
<tag name="patch-summary-total-cves">5</tag>
<tag name="cpe">cpe:/o:microsoft:windows</tag>
<tag name="operating-system">Microsoft Windows 7 Enterprise Service Pack 1</tag>
</HostProperties>
</ReportHost>
</Report>
我尝试了很多方法,但这里是最后两个:
XNamespace ns = "http://www.nessus.org/cm";
var operatingSystem = (findings.Where(a => a.Attribute("name").Value == "operating-system")
.Select(a => a.Value));
var operatingSystem = findings.Descendants().Where(x => x.Attribute("name").Value == ns + "operating-system");
也试过这个解决方案:Use Linq to Xml with Xml namespaces
我从 Microsoft 教程中获得了此方法,但它只有 returns true/false,我无法对其进行操作以将值 Microsoft Windows 7 Enterprise Service Pack 1
分配给 os
变量。
XElement xelement = XElement.Load(s);
IEnumerable<XElement> findings = xelement.Elements();
var hostProperties = from hp in findings.Descendants("HostProperties")
select new
{
os = (string)hp.Element("tag").Attribute("name").Value == "operating-system",
};
我试过使用其他各种 Linq-to-Xml 查询和上面的 where
子句,但它们都是 return Null
或 no values to enumerate
.
在您的情况下,您不需要应用命名空间:
var result = XDocument.Load("data.xml")
.Descendants("tag")
.Where(e => e.Attribute("name").Value == "operating-system")
.FirstOrDefault()?.Value;
//result = Microsoft Windows 7 Enterprise Service Pack 1
多读一点 here 您实际上可以看到,即使您在 xml none 元素中定义了一个命名空间,您的元素也会使用它。您定义的名称空间 (xmlns:cm
) 仅适用于具有 cm:
前缀的元素,它们不是它们。
请注意,如果我按如下方式更改您的 xml(命名空间仅适用于 xmlns
而不是 xmlns:cm
):
<Report name="Black Workstation" xmlns="http://www.nessus.org/cm">
<ReportHost name="192.168.1.000">
<HostProperties>
<tag name="HOST_END">Wed Jun 29 10:32:54 2016</tag>
<tag name="LastAuthenticatedResults">1467214374</tag>
<tag name="patch-summary-total-cves">5</tag>
<tag name="cpe">cpe:/o:microsoft:windows</tag>
<tag name="operating-system">Microsoft Windows 7 Enterprise Service Pack 1</tag>
</HostProperties>
</ReportHost>
</Report>
上面的代码会returnnull
而你要这样写:
XNamespace ns = "http://www.nessus.org/cm";
var result = XDocument.Load("data.xml")
.Descendants(ns + "tag")
.Where(e => e.Attribute("name").Value == "operating-system")
.FirstOrDefault()?.Value;
请尝试以下操作
XDocument document = XDocument.Load("content.xml");
var name = document.XPathSelectElement("Report/ReportHost/HostProperties/tag[@name='operating-system']").Value;