使用 XmlDocument.CreateElement() 创建带有命名空间的 XML 元素

Fra*_*k X 6 .net c# xml namespaces

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

document.DocumentElement.SetAttribute(
    "xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope");
Run Code Online (Sandbox Code Playgroud)

当我创建一个新的时XmlElement使用:

document.createElement("soapenv:Header");
Run Code Online (Sandbox Code Playgroud)

...它不包括soapenv最终 XML 中的名称空间。有什么想法为什么会发生这种情况吗?

更多信息:

好吧,我会尝试稍微澄清一下这个问题。我的代码是:

XmlDocument document = new XmlDocument();
XmlElement element = document.CreateElement("foo:bar");
document.AppendChild(element); Console.WriteLine(document.OuterXml);
Run Code Online (Sandbox Code Playgroud)

输出:

document.DocumentElement.SetAttribute(
    "xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope");
Run Code Online (Sandbox Code Playgroud)

然而,我想要的是:

document.createElement("soapenv:Header");
Run Code Online (Sandbox Code Playgroud)

Dav*_*dRR 5

bar您可以使用XmlDocument.CreateElement 方法(String、String、String)为元素分配命名空间

例子:

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);
Run Code Online (Sandbox Code Playgroud)

预期输出#1:

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);
Run Code Online (Sandbox Code Playgroud)

对于更有趣的示例,请在之前插入这些语句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);
Run Code Online (Sandbox Code Playgroud)

预期输出#2:

<foo:bar xmlns:foo="http://tempuri.org/foo" />
Run Code Online (Sandbox Code Playgroud)

请注意,子元素bizz和 的buzz前缀是命名空间前缀foo,并且命名空间 URIhttp://tempuri.org/foo不会在子元素上重复,因为它是在父元素中定义的bar


小智 -1

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

然而,据我了解,你想做的事情看起来像:

    <?xml version="1.0"?>
    <soapMessage xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope">
        <Header xmlns="http://schemas.xmlsoap.org/soap/envelope" />
    </soapMessage>
Run Code Online (Sandbox Code Playgroud)

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

    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));
Run Code Online (Sandbox Code Playgroud)