XCCDF 上的 C# Linq-to-XML

Chr*_*ris 4 c# xml linq linq-to-xml

我正在尝试从 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());
}
Run Code Online (Sandbox Code Playgroud)

我也试过:

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;
Run Code Online (Sandbox Code Playgroud)

这是 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>
Run Code Online (Sandbox Code Playgroud)

Cha*_*ger 5

双方Benchmark title拥有命名空间http://checklists.nist.gov/xccdf/1.1,所以你需要使用这个,如果你要查询title

其次,您解析 using XElement.Parse,因此结果是表示该Benchmark元素的元素。然后你会得到它的子元素(statusto Value)。然后你搜索任何被调用者的后代Benchmark- 你不会找到任何,就像Benchmark你开始的地方一样。

这应该有效:

var element = XElement.Load(s);

var findingDetails = new
{
    title = (string)element.Element(ns + "title")
};
Run Code Online (Sandbox Code Playgroud)

或者,作为文档加载:

var doc = XDocument.Load(s);

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