【问题标题】:Creating an XML element with a namespace with XmlDocument.CreateElement()使用 XmlDocument.CreateElement() 创建具有命名空间的 XML 元素
【发布时间】:2014-04-09 12:41:14
【问题描述】:

我正在尝试使用 C# 和 .NET(版本 2.0.. 是的,版本 2.0)创建 XmlDocument。我已使用以下方法设置命名空间属性:

document.DocumentElement.SetAttribute(
    "xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope");

当我创建一个新的XmlElement 时:

document.createElement("soapenv:Header");

...它在最终 XML 中不包含 soapenv 命名空间。任何想法为什么会发生这种情况?

更多信息:

好的,我会试着澄清一下这个问题。我的代码是:

XmlDocument document = new XmlDocument();
XmlElement element = document.CreateElement("foo:bar");
document.AppendChild(element); Console.WriteLine(document.OuterXml);

输出:

<bar />

但是,我想要的是:

<foo:bar />

【问题讨论】:

    标签: c# .net xml namespaces


    【解决方案1】:

    您可以使用XmlDocument.CreateElement Method (String, String, String) 将命名空间分配给您的bar 元素

    示例:

    using System;
    using System.Xml;
    
    XmlDocument document = new XmlDocument();
    
    // "foo"                    => namespace prefix
    // "bar"                    => element local name
    // "http://tempuri.org/foo" => namespace URI
    
    XmlElement element = document.CreateElement(
        "foo", "bar", "http://tempuri.org/foo");
    
    document.AppendChild(element);
    Console.WriteLine(document.OuterXml);
    

    预期输出 #1:

    <foo:bar xmlns:foo="http://tempuri.org/foo" />
    

    举个更有趣的例子,在document.AppendChild(element);之前插入这些语句:

    XmlElement childElement1 = document.CreateElement("foo", "bizz",
        "http://tempuri.org/foo");
    
    element.AppendChild(childElement1);
        
    XmlElement childElement2 = document.CreateElement("foo", "buzz",
        "http://tempuri.org/foo");
    
    element.AppendChild(childElement2);
    

    预期输出 #2:

    <foo:bar xmlns:foo="http://tempuri.org/foo"><foo:bizz /><foo:buzz /></foo:bar>
    

    请注意,子元素 bizzbuzz 以命名空间前缀 foo 为前缀,并且命名空间 URI http://tempuri.org/foo 不会在子元素上重复,因为它是在父元素 @ 中定义的987654332@.

    【讨论】:

    • 这很好,但我正在尝试重用在文档根级别定义的命名空间前缀。我已经尝试了很多东西,但我无法让它发挥作用。它总是重新定义子元素本身的命名空间,然后也添加前缀(如您的示例中所示)。任何想法如何只保留前缀而不重复命名空间(已经定义)?
    • @user2173353 我已经演示了如何将子元素附加到父元素,这样父元素的命名空间 URI 不会在子元素上重复。
    • 谢谢!我试图在 XML 文档上实现这一点,但名称空间重复。不知道为什么,但我会将我的代码与您的代码进行比较以找出答案。 ;)
    • 在我的例子中,root 具有命名空间,但我切换了子元素参数(在您的示例中为“buzz”、“foo”、ns:)。将它们切换回“foo”,“buzz”解决了这个问题。谢谢!
    【解决方案2】:

    也许您可以分享您期望的最终 XML 文档。

    但是,据我了解,您想要这样做:

        <?xml version="1.0"?>
        <soapMessage xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope">
            <Header xmlns="http://schemas.xmlsoap.org/soap/envelope" />
        </soapMessage>
    

    所以执行此操作的代码是:

        XmlDocument document = new XmlDocument();
        document.LoadXml("<?xml version='1.0' ?><soapMessage></soapMessage>");
        string soapNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
        XmlAttribute nsAttribute = document.CreateAttribute("xmlns","soapenv","http://www.w3.org/2000/xmlns/");
        nsAttribute.Value = soapNamespace;
        document.DocumentElement.Attributes.Append(namespaceAttribute);
        document.DocumentElement.AppendChild(document.CreateElement("Header",soapNamespace));
    

    【讨论】:

    • -1 您不必显式创建xmlns“属性”。那是一个命名空间声明,简单地创建一个使用命名空间的元素将导致属性神奇地出现。
    • 好的,我试着澄清一下这个问题。我的代码是 XmlDocument document = new XmlDocument(); XmlElement element = document.CreateElement("foo:bar"); document.AppendChild(元素); Console.WriteLine(document.OuterXml); ...它会输出 ...它应该输出
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 2013-02-18
    相关资源
    最近更新 更多