如何基于含命名空间和属性的XML文件生成对应的C# Model类
基于带命名空间的XML生成C#模型类方案
方案一:使用官方工具自动生成(推荐,不易出错)
你可以直接用.NET 自带的架构生成工具自动生成完整模型类,步骤如下:
- 将你提供的XML内容保存为
soap.xml文件 - 打开Visual Studio开发者命令提示符,或dotnet CLI环境:
- 若是.NET Framework环境,执行命令生成XSD架构文件:
xsd.exe soap.xml /outputdir:. - 若是.NET Core/.NET 5+环境,先安装工具:
dotnet tool install --global dotnet-xsd,再执行生成命令:dotnet xsd soap.xml /outputdir:.
- 若是.NET Framework环境,执行命令生成XSD架构文件:
- 基于生成的XSD文件生成C#类:
- .NET Framework:
xsd.exe soap.xsd /classes /language:CS /outputdir:. - .NET Core/.NET 5+:
dotnet xsd soap.xsd /classes /language:CS /outputdir:.
生成的类会自动处理所有命名空间映射、可空属性、Getter/Setter方法,直接拿来用即可。
- .NET Framework:
方案二:手动编写模型类
如果需要手动控制类结构,首先需要引用命名空间System.Xml.Serialization,XML中的前缀(s、a、i等)只是命名空间别名,你只需要在类的特性上绑定别名对应的完整命名空间地址即可,不需要把前缀写到类名里。
完整模型类代码如下:
using System.Xml.Serialization; /// <summary> /// 对应s:Envelope根节点 /// </summary> [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; } } [XmlRoot(Namespace = "http://www.cdiscount.com")] public class GetAllowedCategoryTree { [XmlElement(ElementName = "headerMessage")] public HeaderMessage HeaderMessage { get; set; } } public class HeaderMessage { [XmlElement(ElementName = "Context", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")] public Context Context { get; set; } [XmlElement(ElementName = "Localization", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")] public Localization Localization { get; set; } [XmlElement(ElementName = "Security", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")] public Security Security { get; set; } [XmlElement(ElementName = "Version", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")] public string Version { get; set; } } public class Context { [XmlElement(ElementName = "CatalogID", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")] public int CatalogID { get; set; } [XmlElement(ElementName = "CustomerPoolID", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")] public int CustomerPoolID { get; set; } [XmlElement(ElementName = "SiteID", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")] public int SiteID { get; set; } } public class Localization { [XmlElement(ElementName = "Country", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")] public string Country { get; set; } [XmlElement(ElementName = "Currency", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")] public string Currency { get; set; } [XmlElement(ElementName = "DecimalPosition", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")] public int DecimalPosition { get; set; } [XmlElement(ElementName = "Language", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")] public string Language { get; set; } } public class Security { [XmlElement(ElementName = "DomainRightsList", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages", IsNullable = true)] public string DomainRightsList { get; set; } [XmlElement(ElementName = "IssuerID", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages", IsNullable = true)] public string IssuerID { get; set; } [XmlElement(ElementName = "SessionID", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages", IsNullable = true)] public string SessionID { get; set; } [XmlElement(ElementName = "SubjectLocality", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages", IsNullable = true)] public string SubjectLocality { get; set; } [XmlElement(ElementName = "TokenId", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")] public string TokenId { get; set; } [XmlElement(ElementName = "UserName", Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages", IsNullable = true)] public string UserName { get; set; } }
序列化/反序列化注意事项
如果需要序列化模型为指定格式的XML,需要提前配置命名空间和前缀的映射关系,示例代码:
var ns = new XmlSerializerNamespaces(); ns.Add("s", "http://schemas.xmlsoap.org/soap/envelope/"); ns.Add("a", "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages"); ns.Add("i", "http://www.w3.org/2001/XMLSchema-instance"); // 序列化时传入ns参数即可生成带对应前缀的XML var serializer = new XmlSerializer(typeof(Envelope)); using var writer = new StreamWriter("output.xml"); serializer.Serialize(writer, new Envelope(), ns);
内容的提问来源于stack exchange,提问作者Daniel Onica
相关产品推荐
相关产品推荐

