C# |如何更改 XML 文档中的命名空间?

Posted

技术标签:

【中文标题】C# |如何更改 XML 文档中的命名空间?【英文标题】:C# | How do I change the namespace in an XML document? 【发布时间】:2021-11-28 23:17:18 【问题描述】:

所以我正在努力更改 XML 中特定节点的命名空间。

这就是我想要实现的目标:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.justapage.com/webservices/">
  <SOAP-ENV:Body>
    <ns1:products_Update>
      <ns1:login>
       ...
      </ns1:login>
      <ns1:products>
        <ns1:product>
          ...
        </ns1:product>
      </ns1:products>
    </ns1:products_Update>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

但结果总是这样:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.justapage.com/webservices/">
  <SOAP-ENV:Body>
    <SOAP-ENV:products_Update>
      <SOAP-ENV:login>
       ...
      </SOAP-ENV:login>
      <SOAP-ENV:products>
        <SOAP-ENV:product>
          ...
        </SOAP-ENV:product>
      </SOAP-ENV:products>
    </SOAP-ENV:products_Update>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这就是我序列化它的方式:

 public static async Task<string> SerializeSendMessage<T>(this T toSerialize)
    
        string returnString;
       
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Encoding = Encoding.UTF8;

        XmlSerializer xsSubmit = new XmlSerializer(typeof(T));
        using (StringWriter sw = new StringWriter())
        using (XmlWriter writer = XmlWriter.Create(sw))
        
          
            var xmlns = new XmlSerializerNamespaces();
            xmlns.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
            xmlns.Add("ns1", "http://www.justapage.com/webservices/");

            xsSubmit.Serialize(writer, toSerialize,xmlns);
            returnString = sw.ToString(); // Your XML
        

        return returnString;
    

班级:

[XmlRoot(ElementName="Envelope",IsNullable = false)]
    public class ArticleEnvelope
    
        [XmlElement(ElementName = "Body",Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body  get; set; 

        [XmlAttribute(AttributeName = "SOAP-ENV")]
        public string SOAPENV  get; set; 

        [XmlAttribute(AttributeName = "ns1")]
        public string Ns1  get; set; 

        [XmlText]
        public string Text  get; set; 
    

    [XmlRoot(ElementName = "Body")]
    public class Body
    
        [XmlElement(ElementName = "products_Update")]
        public ProductsUpdate ProductsUpdate  get; set; 
    

   [XmlRoot(ElementName = "products_Update")]
    public class ProductsUpdate
    

        [XmlElement(ElementName = "login")]
        public Login Login  get; set; 

        [XmlElement(ElementName = "products")]
        public Products Products  get; set; 
    

[XmlRoot(ElementName = "products")]
 public class Products
    
     
        [XmlElement(ElementName = "product")]
        public List<Product> Product  get; set; 
    

我试图找到一些关于此的信息,但没有成功,我现在希望这里的人可以为我指出如何实现这一目标的正确方向。

【问题讨论】:

好吧,向我们展示您使用的类型 T 的类以及您填充的该类的实例。 对于[XmlElement(ElementName = "products_Update")],您似乎更希望拥有[XmlElement(ElementName = "products_Update", Namespace = "http://www.justapage.com/webservices/")] 【参考方案1】:

尝试以下:

using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace ConsoleApp2

    class Program
    
        static ArticleEnvelope envelope  get; set; 
        static void Main(string[] args)
        
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;


            XmlSerializer xsSubmit = new XmlSerializer(typeof(ArticleEnvelope));
            using (StringWriter sw = new StringWriter())
            using (XmlWriter writer = XmlWriter.Create(sw))
            

                var xmlns = new XmlSerializerNamespaces();
                xmlns.Add("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
                xmlns.Add("ns1", "http://www.justapage.com/webservices/");

                xsSubmit.Serialize(writer, envelope, xmlns);
            
    
    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
    public class ArticleEnvelope
    
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body  get; set; 

        [XmlText]
        public string Text  get; set; 
    

    public class Body
    
        [XmlElement(ElementName = "products_Update", Namespace = "http://www.justapage.com/webservices/")]
        public ProductsUpdate ProductsUpdate  get; set; 
    

    public class ProductsUpdate
    

        [XmlElement(ElementName = "login", Namespace = "http://www.justapage.com/webservices/")]
        public Login Login  get; set; 

        [XmlArray(ElementName = "products", Namespace = "http://www.justapage.com/webservices/")]
        [XmlArrayItem(ElementName = "product", Namespace = "http://www.justapage.com/webservices/")]
        public List<Product> Products  get; set; 
    
    public class Login
    

    

    public class Product
    

    


【讨论】:

谢谢你,这工作完美无缺!只需添加 XmlArray 装饰和 XmlArrayItem 就可以了。 它不会修复命名空间。

以上是关于C# |如何更改 XML 文档中的命名空间?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 C# 中创建具有命名空间的文档

WCF 客户端,XML 命名空间前缀导致空对象

XML中命名空间之间的区别

不是命名空间的 C# XAttribute

更改 XML 节点属性值会导致“命名空间错误”

如何获取命名空间中的所有类?