我正在尝试从 XCCDF 输出文件中解析出信息,但我的 Linq-to-Xml 查询一直返回空值。以下是我迄今为止尝试过的一些方法:

XElement xelement = XElement.Load(s);
IEnumerable<XElement> findings = xelement.Elements();
XNamespace ns = "http://checklists.nist.gov/xccdf/1.1";

var findingDetails = from f in findings.Descendants(ns + "Benchmark")
                     select new
                     {
                         title = f.Element("title").Value
                     };

foreach (var fd in findingDetails)
{
    Console.WriteLine(fd.ToString());
}

我也试过:
var findingDetails = from f in findings.Descendants(ns + "Benchmark")
                     select f;

var findingDetails = from f in findings.Descendants("Benchmark")
                     select new
                     {
                         title = f.Element("title").Value
                     };

var findingDetails = from f in findings.Elements(ns + "Benchmark")
                     select new
                     {
                         title = f.Element("title").Value
                     };

var findingDetails = from f in findings.Elements(ns + "Benchmark")
                     select f;

这是 xccdf.xml 文件的精简版本。基于此版本,我将如何获得标题“Red Hat...”(第 5 行)和标题“daemon umask”(第 19 行)? (我明白我上面的例子并没有试图获取这些数据,我不得不将它分解为只是试图获取任何东西......
<?xml version="1.0" encoding="UTF-8"?>

<cdf:Benchmark style="SCAP_1.1" resolved="1" id="RHEL_6_STIG" xsi:schemaLocation="http://checklists.nist.gov/xccdf/1.1 http://nvd.nist.gov/schema/xccdf-1.1.4.xsd http://cpe.mitre.org/dictionary/2.0 http://scap.nist.gov/schema/cpe/2.2/cpe-dictionary_2.2.xsd" xmlns:cdf="http://checklists.nist.gov/xccdf/1.1" xmlns:cpe="http://cpe.mitre.org/dictionary/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <cdf:status date="2016-04-22">accepted</cdf:status>
  <cdf:title>Red Hat Enterprise Linux 6 Security Technical Implementation Guide</cdf:title>
  <cdf:description>The Red Hat Enterprise Linux 6 Security Technical Implementation Guide (STIG) is published as a tool to improve the security of Department of Defense (DoD) information systems.  Comments or proposed revisions to this document should be sent via e-mail to the following address: disa.stig_spt@mail.mil.</cdf:description>
  <cdf:notice id="terms-of-use"></cdf:notice>
  <cdf:reference href="http://iase.disa.mil">
        <dc:publisher>DISA</dc:publisher>
        <dc:source>STIG.DOD.MIL</dc:source>
  </cdf:reference>
  <cdf:plain-text id="release-info">Release: 11 Benchmark Date: 22 Apr 2016</cdf:plain-text>
  <cdf:platform idref="cpe:/o:redhat:enterprise_linux:6"></cdf:platform>
  <cdf:version>1</cdf:version>
  <cdf:Profile id="MAC-1_Classified">
        <cdf:title>I - Mission Critical Classified</cdf:title>
  </cdf:Profile>
  <cdf:Value id="var_umask_for_daemons">
        <cdf:title>daemon umask</cdf:title>
        <cdf:description>Enter umask for daemons</cdf:description>
        <cdf:value>022</cdf:value>
        <cdf:value selector="022">022</cdf:value>
        <cdf:value selector="027">027</cdf:value>
  </cdf:Value>
</cdf:Benchmark>

最佳答案

Benchmarktitle 都有命名空间 http://checklists.nist.gov/xccdf/1.1 ,所以如果你要查询 title ,你需要使用它。

其次,您使用 XElement.Parse 进行解析,因此结果是一个表示 Benchmark 元素的元素。然后你得到它的子元素( statusValue )。然后您搜索任何名为 Benchmark 的后代 - 您不会找到任何,因为 Benchmark 是您开始的地方。

这应该有效:

var element = XElement.Load(s);

var findingDetails = new
{
    title = (string)element.Element(ns + "title")
};

或者,作为文档加载:
var doc = XDocument.Load(s);

var findingDetails =
    from benchmark in doc.Descendants(ns + "Benchmark")
    select new
    {
        title = (string)benchmark.Element(ns + "title")
    };

关于XCCDF 上的 C# Linq-to-XML,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39225886/

10-12 16:00