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

本文关键字:命名空间 XML 元素 创建 XmlDocument CreateElement 使用 | 更新日期: 2023-09-27 17:57:38

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

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

当我使用创建新的XmlElement

document.createElement("soapenv:Header");

它在最终的XML中不包括CCD_ 3名称空间。你知道为什么会这样吗?

更多信息:

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

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

输出:

<bar />

然而,我想要的是:

<foo:bar />

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

您可以使用XmlDocument.CreateElement方法(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不在子元素上重复,因为它是在父元素bar内定义的。

using System;
using System.Xml;
namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var Ruta = @"C:'Users'isaac'Documents'XML'XMLTEST.xml";
            Agregar(Ruta, "32323");
        }
        public static void Agregar(string Ruta, string valor)
        {
            XmlDocument XML = new XmlDocument();
            XML.Load(Ruta);
            XmlNode root = XML.GetElementsByTagName("dte:DatosEmision")[0];
            string dte = "http://www.sat.gob.gt/dte/fel/0.2.0";
            XmlElement childElement = XML.CreateElement("dte", "Otros", dte);
            XmlElement childElement1 = XML.CreateElement("dte", "OtroTexto", dte);
            childElement1.SetAttribute("codigo", "OC");
            childElement1.InnerText = valor;
            ////
            childElement.AppendChild(childElement1);
            try
            {

                root.InsertAfter(childElement, root.LastChild);
            }
            catch (Exception x)
            {
                Console.WriteLine(x);
            }
            XML.Save("XMLFile2.xml");
        }
    }
}

结果

    <dte:Otros>
      <dte:OtroTexto codigo="OC">32323</dte:OtroTexto>
    </dte:Otros>

也许您可以共享您期望的最终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));