首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何选择带有XPath的类别XML节点,而不考虑具体的Id?

如何选择带有XPath的类别XML节点,而不考虑具体的Id?
EN

Stack Overflow用户
提问于 2013-03-06 13:09:57
回答 1查看 439关注 0票数 1

在C#中,我使用以下代码将XML文件读入到XmlDocument中。在这样做的时候,我试图选择所有的“类别”节点,而不考虑它们使用XPath的特定类别id。我总共有200多个类别,因此逐个选择它们并不是一个真正的选择。

我尝试过无数的"category“、"/category”、"//catalog/category“等变体,但是nodeList总是返回null。我还试着关注下面的网站和其他许多网站。任何帮助或指导都将不胜感激。

w3 schools

XPath Examples - MSDN

代码语言:javascript
复制
var doc = new XmlDocument();
doc.Load(filename);

XmlNodeList nodeList;
XmlNode root = doc.DocumentElement;

nodeList = root.SelectNodes("/catalog/category")



// Sample XML Document 

<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="http://www.site.com/catalog/2012-10-31" catalog-id="sports">
    <header>
        <image-settings>
            <internal-location base-path="/"/>
            <view-types>
                <view-type>large</view-type>
                <view-type>small</view-type>
                <view-type>swatch</view-type>
            </view-types>
            <alt-pattern>${productname}</alt-pattern>
            <title-pattern>${productname}</title-pattern>
        </image-settings>
    </header>

    <category category-id="root">
        <display-name xml:lang="x-default">Store Catalog</display-name>
        <description xml:lang="x-default">Holds categories for Store</description>
        <online-flag>true</online-flag>
        <template/>
        <page-attributes>
            <page-title xml:lang="x-default">Store Catalog</page-title>
            <page-description xml:lang="x-default">Welcome to the Store</page-description>
        </page-attributes>
        <custom-attributes>
            <custom-attribute attribute-id="enableCompare">false</custom-attribute>
            <custom-attribute attribute-id="showInMenu">false</custom-attribute>
        </custom-attributes>
        <refinement-definitions>
            <refinement-definition type="category" bucket-type="none">
                <display-name xml:lang="x-default">Category</display-name>
                <sort-mode>category-position</sort-mode>
                <cutoff-threshold>5</cutoff-threshold>
            </refinement-definition>
        </refinement-definitions>
    </category>

    <category category-id="whats-new">
        <display-name xml:lang="x-default">NEW</display-name>
        <online-flag>true</online-flag>
        <parent>root</parent>
        <position>25.0</position>
        <template>rendering/category/categoryproducthits</template>
        <page-attributes>
            <page-title xml:lang="x-default">What's New - New at Store</page-title>
            <page-description xml:lang="x-default">Learn what's new at the store.</page-description>
        </page-attributes>
        <custom-attributes>
            <custom-attribute attribute-id="contentOnly">false</custom-attribute>
            <custom-attribute attribute-id="displaySubNav">false</custom-attribute>
            <custom-attribute attribute-id="dropdownNavSlot1">dropdown-left7</custom-attribute>
            <custom-attribute attribute-id="enableCompare">false</custom-attribute>
            <custom-attribute attribute-id="enableslotat3">true</custom-attribute>
            <custom-attribute attribute-id="enableslotat4">true</custom-attribute>
            <custom-attribute attribute-id="enableslotat9">true</custom-attribute>
            <custom-attribute attribute-id="showInMenu">true</custom-attribute>
            <custom-attribute attribute-id="showShadeSelector">false</custom-attribute>
            <custom-attribute attribute-id="showSubCollectionsAsTabs">false</custom-attribute>
        </custom-attributes>
    </category>

// cut for brevity - the above category nodes are the same format as the rest of the XML document.
EN

回答 1

Stack Overflow用户

发布于 2013-03-08 17:41:22

由于XML使用显式名称空间,因此似乎必须在SelectNodes方法中指定它:

代码语言:javascript
复制
 XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
 nsmgr.AddNamespace("cc", "http://www.site.com/catalog/2012-10-31");
 XmlNodeList nodelist = doc.SelectNodes("/cc:catalog/cc:category", nsmgr);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15239425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档