代码之家  ›  专栏  ›  技术社区  ›  jwaliszko

对命名空间使用xpath时出现无效的标记错误

  •  1
  • jwaliszko  · 技术社区  · 14 年前

    我有以下XML:

    <ns:response xmlns:ns="http://example.com" xmlns:ax="http://example.com/xsd" >
        <ns:return type="mytype">
            <ax:roleID>1</ax:roleID>
            <ax:roleName>ADM</ax:roleName>
        </ns:return>
        <ns:return type="mytype">
            <ax:roleID>2</ax:roleID>
            <ax:roleName>USR</ax:roleName>
        </ns:return>
    </ns:response>
    

    获取所有角色名(adm、usr)的xpath表达式是什么样的?

    这不起作用:

    ns:response/ns:return/ax:roleName ns http://example.com ax http://example.com/xsd
    

    当我使用它时,我得到了例外

    'ns:响应/ns:返回/ax:rolename ns http://example.com 斧头 http://example.com/xsd '具有无效的标记。

    2 回复  |  直到 12 年前
        1
  •  2
  •   Hinek    14 年前

    如果您正在使用 XmlDocument.SelectNodes 方法,您应该使用 "ns:response/ns:return/ax:roleName" 作为xpath并将命名空间添加到 XmlNamespaceManager :

    man.AddNamespace("ns", "http://example.com");
    man.AddNamespace("ax", "http://example.com/xsd");
    var set = doc.SelectNodes("ns:response/ns:return/ax:roleName", man);
    
        2
  •  0
  •   Paul Michaels    14 年前

    我认为您只需要指定ns的incex:返回,因为有多个:

    ns:response/ns:return[1]/ax:roleName