<bdo id='7wxTW'></bdo><ul id='7wxTW'></ul>

      <legend id='7wxTW'><style id='7wxTW'><dir id='7wxTW'><q id='7wxTW'></q></dir></style></legend>

      <tfoot id='7wxTW'></tfoot>
      <i id='7wxTW'><tr id='7wxTW'><dt id='7wxTW'><q id='7wxTW'><span id='7wxTW'><b id='7wxTW'><form id='7wxTW'><ins id='7wxTW'></ins><ul id='7wxTW'></ul><sub id='7wxTW'></sub></form><legend id='7wxTW'></legend><bdo id='7wxTW'><pre id='7wxTW'><center id='7wxTW'></center></pre></bdo></b><th id='7wxTW'></th></span></q></dt></tr></i><div id='7wxTW'><tfoot id='7wxTW'></tfoot><dl id='7wxTW'><fieldset id='7wxTW'></fieldset></dl></div>

      <small id='7wxTW'></small><noframes id='7wxTW'>

    1. 如何选择使用默认命名空间的节点?

      时间:2023-06-04
      1. <tfoot id='6EwNr'></tfoot>

          <bdo id='6EwNr'></bdo><ul id='6EwNr'></ul>
        • <i id='6EwNr'><tr id='6EwNr'><dt id='6EwNr'><q id='6EwNr'><span id='6EwNr'><b id='6EwNr'><form id='6EwNr'><ins id='6EwNr'></ins><ul id='6EwNr'></ul><sub id='6EwNr'></sub></form><legend id='6EwNr'></legend><bdo id='6EwNr'><pre id='6EwNr'><center id='6EwNr'></center></pre></bdo></b><th id='6EwNr'></th></span></q></dt></tr></i><div id='6EwNr'><tfoot id='6EwNr'></tfoot><dl id='6EwNr'><fieldset id='6EwNr'></fieldset></dl></div>

          <small id='6EwNr'></small><noframes id='6EwNr'>

              <legend id='6EwNr'><style id='6EwNr'><dir id='6EwNr'><q id='6EwNr'></q></dir></style></legend>
                <tbody id='6EwNr'></tbody>
                本文介绍了如何选择使用默认命名空间的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                XML文件的结构大致如下:

                The structure of the XML file is more or less as follows:

                <?xml version="1.0" encoding="UTF-8"?>
                <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="url1" xsi:schemaLocation="url2 url3">
                   <b>
                     <c></c>
                     <c></c>
                     <c></c>
                   </b>
                </a>
                

                我的目标是选择所有c"元素,但以下 xpath 表达式不起作用://a/b/c".

                My goal is to select all the "c" elements, but the following xpath expression won't work: "//a/b/c".

                即:

                XmlDocument doc= new XmlDocument();
                doc.Load(filepath);
                XmlNodeList l = doc.SelectNodes("//a/b/c"); // 0 nodes
                

                我测试过的唯一有效的 xpath 表达式是/*(1 个节点)和//*(所有节点).

                The only xpath expressions I tested that worked are /* (1 node) and //* (all nodes).

                这个问题与 XML 命名空间有关吗?如果是这样,设置 XMLDocument 对象的正确方法是什么?

                Is this problem related to the XML namespace? If so, what's the proper way to set up the XMLDocument object?

                        XmlDocument doc= new XmlDocument();
                        doc.Load(filepath);
                        XmlNamespaceManager m = new XmlNamespaceManager(doc.NameTable);
                        m.AddNamespace(/* what goes here? */);
                        XmlNodeList l = doc.SelectNodes("//a/b/c", m);
                

                推荐答案

                你需要为文档使用的默认命名空间分配一个命名空间前缀,然后在你的 XPath 中使用它:

                You need to assign a namespace prefix for the default namespace that the document is using, and then use that in your XPath:

                XmlDocument doc= new XmlDocument();
                doc.Load(filepath);
                
                XmlNamespaceManager m = new XmlNamespaceManager(doc.NameTable);
                m.AddNamespace("myns", "url1");
                
                XmlNodeList l = doc.SelectNodes("/myns:a/myns:b/myns:c", m);
                

                您可以将前缀myns"替换为基本上任何内容(不带空格的字母数字),只要它在第 4 行和 XPath 之间保持一致,并且正确分配给第 4 行中的url1"命名空间.

                You can replace the prefix "myns" with essentially anything (alphanumeric without spaces), as long as it's consistent between line 4 and the XPath, and that it's correctly assigned to the "url1" namespace in line 4.

                这篇关于如何选择使用默认命名空间的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:允许 XSD 日期元素为空字符串 下一篇:多次导入同一个 XSD 会出错吗?

                相关文章

                最新文章

                1. <tfoot id='X1Ugv'></tfoot>

                      <bdo id='X1Ugv'></bdo><ul id='X1Ugv'></ul>
                    <i id='X1Ugv'><tr id='X1Ugv'><dt id='X1Ugv'><q id='X1Ugv'><span id='X1Ugv'><b id='X1Ugv'><form id='X1Ugv'><ins id='X1Ugv'></ins><ul id='X1Ugv'></ul><sub id='X1Ugv'></sub></form><legend id='X1Ugv'></legend><bdo id='X1Ugv'><pre id='X1Ugv'><center id='X1Ugv'></center></pre></bdo></b><th id='X1Ugv'></th></span></q></dt></tr></i><div id='X1Ugv'><tfoot id='X1Ugv'></tfoot><dl id='X1Ugv'><fieldset id='X1Ugv'></fieldset></dl></div>
                  1. <legend id='X1Ugv'><style id='X1Ugv'><dir id='X1Ugv'><q id='X1Ugv'></q></dir></style></legend>

                    <small id='X1Ugv'></small><noframes id='X1Ugv'>