You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将带命名空间的XML转换为C# Model类并实现动态序列化

实现方案

核心使用System.Xml.Serialization下的特性注解定义模型,配合XmlSerializer生成带命名空间前缀的XML,完全不需要手动拼接字符串。

第一步:引入依赖命名空间

using System.Xml.Serialization;

第二步:定义所有实体模型

// 对应根节点 s:Envelope
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
    [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public Body Body { get; set; }
}

public class Body
{
    [XmlElement(ElementName = "GetAllowedCategoryTree", Namespace = "http://www.cdiscount.com")]
    public GetAllowedCategoryTree GetAllowedCategoryTree { get; set; }
}

public class GetAllowedCategoryTree
{
    [XmlElement(ElementName = "headerMessage")]
    public HeaderMessage HeaderMessage { get; set; }
}

[XmlRoot(Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")]
public class HeaderMessage
{
    [XmlElement(Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")]
    public Context Context { get; set; } = new();
    
    [XmlElement(Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")]
    public Localization Localization { get; set; } = new();
    
    [XmlElement(Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")]
    public Security Security { get; set; } = new();
    
    [XmlElement(Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")]
    public string Version { get; set; } = "1.0";
}

public class Context
{
    public int SiteID { get; set; } = 100;
}

public class Localization
{
    public string Country { get; set; } = "Fr";
    public string Currency { get; set; } = "Eur";
    public int DecimalPosition { get; set; } = 2;
    public string Language { get; set; } = "Fr";
}

public class Security
{
    [XmlElement(IsNullable = true)]
    public string DomainRightsList { get; set; } = null;
    [XmlElement(IsNullable = true)]
    public string IssuerID { get; set; } = null;
    [XmlElement(IsNullable = true)]
    public string SessionID { get; set; } = null;
    [XmlElement(IsNullable = true)]
    public string SubjectLocality { get; set; } = null;
    public string TokenId { get; set; }
    [XmlElement(IsNullable = true)]
    public string UserName { get; set; } = null;
}

第三步:序列化生成XML代码

// 动态传入token
string token = "你的动态token值";

// 构造对象
var envelope = new Envelope
{
    Body = new Body
    {
        GetAllowedCategoryTree = new GetAllowedCategoryTree
        {
            HeaderMessage = new HeaderMessage
            {
                Security = new Security
                {
                    TokenId = token
                }
            }
        }
    }
};

// 配置命名空间前缀
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("s", "http://schemas.xmlsoap.org/soap/envelope/");
namespaces.Add("a", "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages");
namespaces.Add("i", "http://www.w3.org/2001/XMLSchema-instance");

// 序列化
var serializer = new XmlSerializer(typeof(Envelope));
using var writer = new StringWriter();
serializer.Serialize(writer, envelope, namespaces);
string xmlResult = writer.ToString();

说明

  • 所有固定值都在模型定义时默认赋值,仅需动态修改TokenId属性即可
  • 标注IsNullable = true的属性为null时,序列化会自动生成i:nil="true"属性
  • 生成的XML结构和命名空间与你手动拼接的完全一致

内容的提问来源于stack exchange,提问作者Daniel Onica

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 13:57:04