2013-08-21 154 views
1

我的XML文档看起来是这样的:的XPath选择元素

<?xml version="1.0" encoding="ISO-8859-1"?> 
<XCER xmlns="http://www.x.y.z/base" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.x.y.z/base APSchema.xsd" mp_id="56977" ma_id="398" sub_id="54977"> 
    <final_result OD="DGS=1.7:ADB=2" creator="Px" version="1.1" xsi:type="XCT"> 
    <code>3040280100331</code> 
    <code_cnf>1.0000</code_cnf> 
    <code_attr>seq</code_attr> 
    <status_attr>fdos</status_attr> 
    <text>xxx</text> 
    <standardized_text>xxxx</standardized_text> 
    <region> 
     <type>address</type> 
     <symbology>machine</symbology> 
    </region> 
    <recognized_elements> 
     <type>reg</type> 
     <nominal> 
     <type>reg</type> 
     <code>Dx</code> 
     <code_cnf>0.0635</code_cnf> 
     <location page="3"> 
      <face>rear</face> 
      <polygon> 
      <dot x="720" y="709" /> 
      <dot x="744" y="708" /> 
      <dot x="744" y="591" /> 
      <dot x="720" y="592" /> 
      </polygon> 
     </location> 
     </nominal> 
     <reference> 
     <type>reg</type> 
     <code>Dx</code> 
     <match_cnf>1.0000</match_cnf> 
     <attribute>full_match</attribute> 
     </reference> 
    </recognized_elements> 
    <recognized_elements> 
     <type>cty</type> 
     <reference> 
     <type>cty</type> 
     <code>Dx 9</code> 
     <attribute>derived</attribute> 
     </reference> 
    </recognized_elements> 
    <standardized_elements> 
     <type>reg</type> 
     <code>Dx</code> 
    </standardized_elements> 
    <standardized_elements> 
     <type>cty</type> 
     <code>Dx 9</code> 
     <attribute>err</attribute> 
    </standardized_elements> 
    </final_result> 
</XCER> 

我想要做一些事情吧,我无法弄清楚使用XPath如何,我可以找到在我找到的例子中,我需要特定的案例。我的问题是,我想找到“DXF”,这是在Recognized_elements:参考:代码,但只有当是“章”和IS full_match>

我不工作的表达式如下:

doc.SelectSingleNode("/x:XCER[0]/x:final_result[@creator = 'Px']/x:recognized_elements/x:reference[type='reg' and attribute='full_match]/x:code", nsmgr).InnerText 

错误我总是得到如下: “对象引用不设置到对象的实例”

+1

为什么'x:result'而不是'x:final_result'? –

+0

对不起,现在我改变了原来的拼写错误 – foreachin

+1

您还在寻找'XCER'的节点1,而根节点是节点0.'x:recognised_elements'没有紧接着的'attribute'元素它要么(它在'reference'下)。 –

回答

1

我想你需要的是这样的:

doc.SelectSingleNode("/x:XCER/x:final_result[@x:creator = 'Px']/x:recognized_elements[x:type='reg' and x:reference/x:attribute='full_match']/x:reference/x:code", nsmgr).InnerText 

你在几个地方错过了命名空间,你忘了把完整的路径放到一些子元素上。我不知道您是否打算读取reference/code元素或nominal/code元素。

+1

这是选择中缺少的命名空间让我感谢,谢谢 – foreachin