2016-08-30 21 views
3

私はXCCDF出力ファイルから情報を解析しようとしていますが、Linq-to-Xmlクエリは空のまま戻り続けます。ここで私は、この時点まで試したもののいくつかされていますXCCDF上のC#Linq-to-XML

​​

私も試してみました:

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: [email protected]</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> 

答えて

4

両方Benchmark次試してみてください...私はちょうど何かを取得しようとし、それを打破しなければならなかった、上記の私の例では、このデータを取得しようとしていないことを理解しませんあなたがtitleを照会しようとしている場合は、これを使用する必要があるようにtitleは、名前空間http://checklists.nist.gov/xccdf/1.1を持っている。

第二に、あなたは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") 
    }; 
+0

あなたの助けてくれてありがとう、私は昨夜、私はあるポイントを考えていましたが、私はこれらのラインに沿って何かを試しましたが、何かを見逃しているに違いありません。 – Chris

1

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ProgrammingBasics 
{ 
    class Exercise 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main() 
     { 
      XDocument doc = XDocument.Load(FILENAME); 
      var results = doc.Descendants().Where(x => x.Name.LocalName == "Benchmark").Select(y => new 
      { 
       title = (string)y.Elements().Where(z => z.Name.LocalName == "title").FirstOrDefault() 
      }).FirstOrDefault(); 
     } 

    } 

} 
+0

がnull、私はコードがポストされたXML名前空間で動作 – Chris

+0

を追加しても。私は完全にテストしました。名前空間は必要ではなく、コードが失敗する可能性があります。 – jdweng

関連する問題