c# - Why I can't access to these node content using XPath? -


i pretty new in xpath , in c# , have following problem:

i have parse file: http://static.nvd.nist.gov/feeds/xml/cpe/dictionary/official-cpe-dictionary_v2.3.xml

as can see opening in browser file have following structure:

<?xml version='1.0' encoding='utf-8'?> <cpe-list xmlns:meta="http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2" xmlns:config="http://scap.nist.gov/schema/configuration/0.1" xmlns:ns6="http://scap.nist.gov/schema/scap-core/0.1" xmlns:scap-core="http://scap.nist.gov/schema/scap-core/0.3" xmlns="http://cpe.mitre.org/dictionary/2.0" xmlns:cpe-23="http://scap.nist.gov/schema/cpe-extension/2.3" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://scap.nist.gov/schema/configuration/0.1 http://nvd.nist.gov/schema/configuration_0.1.xsd http://cpe.mitre.org/dictionary/2.0 http://scap.nist.gov/schema/cpe/2.3/cpe-dictionary_2.3.xsd http://scap.nist.gov/schema/scap-core/0.3 http://nvd.nist.gov/schema/scap-core_0.3.xsd http://scap.nist.gov/schema/scap-core/0.1 http://nvd.nist.gov/schema/scap-core_0.1.xsd http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2 http://nvd.nist.gov/schema/cpe-dictionary-metadata_0.2.xsd http://scap.nist.gov/schema/cpe-extension/2.3 http://scap.nist.gov/schema/cpe/2.3/cpe-dictionary-extension_2.3.xsd">   <generator>     <product_name>national vulnerability database (nvd)</product_name>     <product_version>2.22.0-snapshot (production)</product_version>     <schema_version>2.3</schema_version>     <timestamp>2014-03-05t05:13:33.550z</timestamp>   </generator>   <cpe-item name="cpe:/a:1024cms:1024_cms:0.7">     <title xml:lang="en-us">1024cms.org 1024 cms 0.7</title>     <cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:0.7:*:*:*:*:*:*:*"/>   </cpe-item>   <cpe-item name="cpe:/a:1024cms:1024_cms:1.2.5">     <title xml:lang="en-us">1024cms.org 1024 cms 1.2.5</title>     <cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.2.5:*:*:*:*:*:*:*"/>   </cpe-item>   <cpe-item name="cpe:/a:1024cms:1024_cms:1.3.1">     <title xml:lang="en-us">1024cms.org 1024 cms 1.3.1</title>     <cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.3.1:*:*:*:*:*:*:*"/>   </cpe-item>    .............................................................   .............................................................   .............................................................    <cpe-item name="cpe:/h:zyxel:p-660hw_t3:v2">     <title xml:lang="en-us">zyxel p-660hw t3 model v2</title>     <cpe-23:cpe23-item name="cpe:2.3:h:zyxel:p-660hw_t3:v2:*:*:*:*:*:*:*"/>   </cpe-item> </cpe-list> 

so now, using xpath, have obtain list of tag (excluding first tag situated first tag tag

in code have it:

       xmldocument document = new xmldocument();    // represent xml document        document.load(sourcexml.fullname);       // loads xml document specified stream         // add namespaces:        xmlnamespacemanager nsmgr = new xmlnamespacemanager(document.nametable);        nsmgr.addnamespace("ns6", "http://scap.nist.gov/schema/scap-core/0.1");        nsmgr.addnamespace("cpe-23", "http://scap.nist.gov/schema/cpe-extension/2.3");        nsmgr.addnamespace("ns", "http://cpe.mitre.org/dictionary/2.0");        nsmgr.addnamespace("meta", "http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2");        nsmgr.addnamespace("scap-core", "http://scap.nist.gov/schema/scap-core/0.3");        nsmgr.addnamespace("xsi", "http://www.w3.org/2001/xmlschema-instance");        nsmgr.addnamespace("config", "http://scap.nist.gov/schema/configuration/0.1");           /* nodelist collection contains <cpe-item> tag          * inside root <cpe-list> tag in xml document:           */        xmlnodelist nodelist;        nodelist = document.documentelement.selectnodes("//ns:cpe-list/ns:cpe-item", nsmgr);        long conta = 0; 

so using line select tag tag:

nodelist = document.documentelement.selectnodes("//ns:cpe-list/ns:cpe-item", nsmgr); 

it seems work not sure if correct because when using visual studio debugger me xmlnodelist nodelist contains: 80588 element (the file big seems me element !!!)

another doubt related use of ns namespace previouse code (this not code, have work on it).

why in previous code there ns namepace ahead cpe-list , cpe-item if in xml code parse smply have like:

  <cpe-item name="cpe:/a:1024cms:1024_cms:1.3.1">     <title xml:lang="en-us">1024cms.org 1024 cms 1.3.1</title>     <cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.3.1:*:*:*:*:*:*:*"/>   </cpe-item> 

that don't begin ns namespace? why used?

the last question how can access title inner text content?

i trying in way can't work:

xmlnodelist nodelist; nodelist = document.documentelement.selectnodes("//ns:cpe-list/ns:cpe-item", nsmgr); long conta = 0;  datamodel.vulnerability.cpe currentcpe;  foreach (xmlnode node in nodelist) {     // access name attribute of <cpe-item> tag:     debug.writeline(string.format("[{0:n0}] cpe: {1}  title: {2}", conta, node.attributes["name"].value, node.firstchild.firstchild.value));      // access <title> tag content:     //debug.writeline(string.format("[{0:n0}] title: {1}  title: {2}", conta, node.selectsinglenode("./title", nsmgr)));     xmlnode titlenode = node.selectsinglenode("./title", nsmgr);      conta++;  } 

when code executed have no problem access name attributes of current cpe element list can't access content of tag because when execute line:

 xmlnode titlenode = node.selectsinglenode("./title", nsmgr); 

it return value null

what problem? missing? how can solve?

tnx

andrea

  1. your xpath looks fine given xml snippet posted in question. should return correct number of elements far can see. can't tell more that, should check further yourself.
  2. your xml has default namespace (xmlns="....."). elements in xml without prefix considered in default namespace. in xpath, element without prefix considered has no namespace. in end, different paradigm of both platform requires define ns prefix point default namespace url use in xpath statement.
  3. related point 2. remember element without prefix in default namespace. <title> element. hence need add ns prefix in xpath statement : ./ns:title
  4. ideally, one post has contains no more one specific question. answering bunch of questions in 1 post useful future visitors, tend confuse them instead. remember not solving problem here, trying build knowledge-base useful others having similar problem.

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -