如何防止 appendChild() 添加 xmlns=""; [英] How can I prevent appendChild() from adding the xmlns=""

查看:64
本文介绍了如何防止 appendChild() 添加 xmlns="";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码片段.

FilterText = HttpUtility.UrlDecode(FilterTxt.Value.ToString());
               XmlWriterSettings settings = new XmlWriterSettings();
               settings.Indent = true;
               TextWriter tw = new StreamWriter("D:\\FilterTest.rdl");
               tw.WriteLine(FilterText);
               tw.Close();
               XmlDocument reportrdl = new XmlDocument();
               reportrdl.Load(ReportFile);
               NMS = reportrdl.NamespaceURI;
               XmlNodeList fieldsnode = reportrdl.GetElementsByTagName("DataSet");
               //XmlElement xoo = reportrdl.CreateElement("Filters", NMS);
               //reportrdl.AppendChild(xoo);
               foreach (XmlNode fields in fieldsnode)
               {
                   // second document to merge (the new Filter File)

                   XmlDocument filterrdl = new XmlDocument();
                   filterrdl.Load("D:\\FilterTest.rdl");

                   XmlNode imported = reportrdl.ImportNode(filterrdl.DocumentElement, true);

                   fields.AppendChild(imported);
                   break;
               }
               //XmlNodeList filtersnode = reportrdl.GetElementsByTagName("Filters");
               //foreach (XmlNode filters in filtersnode)
               //{
               //    filters.Attributes.RemoveNamedItem("xmlns");
               // }
               reportrdl.Save("D:\\NewFilter.rdl");

这是 rdl 文件的片段:

Here is snippet of the rdl file:

<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
  <DataSources>
    <DataSource Name="BOSS">
      <rd:DataSourceID>c6a8409e-71a4-4e96-86ad-b300a5b942c3</rd:DataSourceID>
      <ConnectionProperties>
        <DataProvider>SQL</DataProvider>
        <ConnectString>dats a no no</ConnectString>
        <IntegratedSecurity>true</IntegratedSecurity>
      </ConnectionProperties>
    </DataSource>
  </DataSources>

<Query>之后在这个文件中进一步向下,我需要在<Filters>

Further on down in this file after <Query>, I need to merge in the <Filters>

这是那个文件:

<Filters>
  <Filter>
    <FilterExpression>=Fields!RoleID.Value</FilterExpression>
    <Operator>Equal</Operator>
    <FilterValues>
      <FilterValue>=27</FilterValue>
    </FilterValues>
  </Filter>
</Filters>

结束 而不是

谢谢

推荐答案

您需要向我们展示您尝试合并的两个文件的 XML 示例.

You need to show us samples of the XML of the two files you're trying to merge.

可能发生的情况是,在第一个文件 reportrdl 中,DataSet 元素位于除 "" 之外的默认命名空间声明下.因此,它们下的每个没有命名空间前缀且未覆盖默认命名空间声明的元素都将位于其祖先声明的默认命名空间中.

What's probably happening is that in the first file, reportrdl, the DataSet elements are under a default namespace declaration other than "". Therefore, every element under them that does not have a namespace prefix and does not override the default namespace declaration will be in the default namespace declared by its ancestor.

但是您要导入不在该命名空间中且没有前缀的 Filters 元素.因此,为了防止他们继承不适用于他们的默认命名空间,他们需要取消声明默认命名空间,这是使用 xmlns="" 完成的.

But you're importing Filters elements that are not in that namespace, and have no prefix. Therefore, in order to prevent them inheriting a default namespace that does not apply to them, they need to undeclare the default namespace, which is done using xmlns="".

如果上述情况并非如此,请提供两个 XML 文档的样本,这样我们就不会试图在黑暗中进行手术.

If the above is not the case, please provide samples of the two XML documents, so we're not trying to do surgery in the dark.

如果上述,那么回到您的问题是,为什么您不希望 xmlns="" 在输出?听起来这是正确的输出.但也许您不想要它,因为您希望导入的 Filters 元素与其父元素位于同一命名空间中;在这种情况下,您需要更改 imported 的命名空间.然后是命名空间声明.

If the above is the case, the question back to you is, why do you not want the xmlns="" in the output? It sounds like it's the correct output. But maybe you don't want it because you want the imported Filters element to be in the same namespace as its parent; in that case, you need to change the namespace of imported. Then the namespace declarations will follow.

或者您可能希望导入的节点不在命名空间中,因此输出在技术上已经是正确的,但是您不喜欢 xmlns="" 的美感.或者可能有一个下游 XML 使用者出于某种原因在 xmlns="" 上窒息.告诉我们更多有关您的限制的信息,以便我们知道如何提供帮助.

Or maybe you want the imported node to be in no namespace, so the output is technically correct already, but you don't like the aesthetics of xmlns="". Or maybe there's a downstream XML consumer that chokes on xmlns="" for some reason. Tell us more about your constraints so we can know how to help.

更新:

好的,现在我们知道了

  • 第一个文件中的元素位于 reportdefinition 命名空间中,其 URL 为 "http://schemas.microsoft.com/sqlserver/reporting/2009/01/reportdefinition".我们可以说是因为顶级元素 上有默认的命名空间声明:xmlns="http://schemas.microsoft.com.../reportdefinition".这意味着对于 和所有后代,任何没有命名空间前缀的元素都被视为在 reportdefinition 命名空间中.
  • 导入文件中的元素,如 应该在同一个 reportdefinition 命名空间中.我从 MS 文档中推断出这一点,虽然不清楚.不幸的是,该文档的大部分内容都很糟糕......充满了病-形成的 XML 甚至!
  • 然而,导入的 <Filters> 元素和后代实际上没有命名空间(在您导入它们之前),因为它们没有命名空间前缀和默认命名空间声明.
  • The elements in the first file are in the reportdefinition namespace, whose URL is "http://schemas.microsoft.com/sqlserver/reporting/2009/01/reportdefinition". We can tell because the top-level element, <Report>, has the default namespace declaration on it: xmlns="http://schemas.microsoft.com.../reportdefinition". This means that for <Report> and all descendants, any element with no namespace prefix is considered to be in the reportdefinition namespace.
  • The elements in the imported file, like <Filters> should be in the same reportdefinition namespace. I infer this from the MS documentation, though it's not clear. Much of this documentation is unfortunately awful ... full of ill-formed XML even!
  • However the imported <Filters> element and descendants are actually in no namespace (before you import them), because they have no namespace prefix and no default namespace declaration.

因此,当您导入 <Filters> 元素时,AppendChild() 必须将其保持在与其所在的相同命名空间中(即没有命名空间) - 否则,就像改名一样.reportdefinition 命名空间中的元素不同.如果 按原样附加在 下,它将从其新祖先 继承默认命名空间声明code>,因此其命名空间将更改为 reportdefinition 命名空间.

So when you import the <Filters> element, AppendChild() must act to keep it in the same namespace that it's in (i.e. no namespace) - otherwise, it would be like changing its name. <Filters> in no namespace is a different element from <Filters> in the reportdefinition namespace. If <Filters> were appended under <DataSet> as-is, it would inherit the default namespace declaration from its new ancestor <Report> and thereby its namespace would be changed to the reportdefinition namespace.

但是,您确实希望它位于 reportdefinition 命名空间中.最简单的方法是更改导入的文件,以便已经在该命名空间中:

However, you do want it to be in the reportdefinition namespace. The easiest way to accomplish that is to change your imported file so that <Filters> is already in that namespace:

<Filters xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition">
  <Filter>

然后,AppendChild() 将看到 已经在其新祖先的默认命名空间中,并且不必取消声明默认命名空间.事实上,它可以删除 上的默认命名空间声明,现在是多余的.

Then, AppendChild() will see that <Filters> is already in the default namespace of its new ancestors, and won't have to undeclare the default namespace. In fact, it can remove the default namespace declaration on <Filters>, which is now redundant.

但是,我们不能保证它会删除它.了解 XML 命名空间的一件重要事情是,重要的是每个元素在什么命名空间.使用什么 prefix 表示该命名空间无关紧要,声明命名空间前缀(或默认命名空间)的位置或次数也无关紧要:只要每个元素以正确的命名空间.

However, we can't guarantee that it will remove it. One important thing to understand about XML namespaces is that what matters is what namespace each element is in. It doesn't matter what prefix is used to indicate that namespace, nor does it matter where or how many times that namespace prefix (or default namespace) is declared: just so long as each element ends up in the right namespace.

因此,您导入的 元素是否最终具有默认命名空间声明 (DND),对于任何合法的 XML 使用者来说都无关紧要.重要的是它在 reportdefinition 命名空间中.如果 Microsoft RDL 处理器在 DND 上阻塞,则意味着该处理器不符合 XML 命名空间标准.

So, whether your imported <Filters> element ends up having a default namespace declaration (DND) or not should not matter to any legitimate XML consumer. All that matters is that it's in the reportdefinition namespace. If a Microsoft RDL processor chokes on the DND, it means that the processor does not adhere to XML namespace standards.

我真的建议您阅读有关 XML 名称空间的简短教程……尽管它们享有盛誉,但它们并没有那么复杂;当您使用标准化的 XML 词汇表时,它们几乎无处不在;并了解声明的工作原理可以为您省去很多麻烦.这个不短但很好.我不能推荐 w3schools 一个,因为它混淆了前缀和命名空间.:-p

I would really recommend reading a short tutorial on XML namespaces... They're not that complicated, despite their reputation; they're almost ubiquitous when you work with standardized XML vocabularies; and understanding how the declarations work can save you a lot of headache. This one's not short but it's good. I can't recommend the w3schools one because it confuses prefixes with namespaces. :-p

哇,时间长了.不过,我觉得这是必要的:我们可以整天放水,但我宁愿堵住泄漏,这是由于不了解 XML 命名空间声明的工作原理造成的.

Wow, that got long. I felt like it was necessary, though: we can bail water all day long, but I'd rather plug the leak, which is caused by not understanding how XML namespace declarations work.

这篇关于如何防止 appendChild() 添加 xmlns="";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆