将带有命名空间和属性的 XML 转换为 C# 模型类
Posted
技术标签:
【中文标题】将带有命名空间和属性的 XML 转换为 C# 模型类【英文标题】:Convert XML with namespaces and attributes into C# Model class 【发布时间】:2021-12-05 00:12:38 【问题描述】:谁能帮我解决以下问题。我想创建一个基于具有命名空间和属性的 xml 文件的 c# 模型类。
这是xml文件:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetAllowedCategoryTree xmlns="http://www.cdiscount.com">
<headerMessage xmlns:a="http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Context>
<a:CatalogID>1</a:CatalogID>
<a:CustomerPoolID>1</a:CustomerPoolID>
<a:SiteID>100</a:SiteID>
</a:Context>
<a:Localization>
<a:Country>Fr</a:Country>
<a:Currency>Eur</a:Currency>
<a:DecimalPosition>2</a:DecimalPosition>
<a:Language>Fr</a:Language>
</a:Localization>
<a:Security>
<a:DomainRightsList i:nil="true" />
<a:IssuerID i:nil="true" />
<a:SessionID i:nil="true" />
<a:SubjectLocality i:nil="true" />
<a:TokenId>$#Project#token</a:TokenId>
<a:UserName i:nil="true" />
</a:Security>
<a:Version>1.0</a:Version>
</headerMessage>
</GetAllowedCategoryTree>
</s:Body>
</s:Envelope>
我不知道如何制作一个 C# 类模型,使用 getter 和 setter 用于类似 xml 的标签
s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/
非常感谢您的关注!
【问题讨论】:
【参考方案1】:尝试以下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication2
class Program
const string FILE = @"c:\TEMP\TEST.XML";
static void Main(string[] args)
XmlReader reader = XmlReader.Create(FILE);
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
Envelope envelope = (Envelope)serializer.Deserialize(reader);
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
public Body Body get; set;
public class Body
[XmlArray(ElementName = "GetAllowedCategoryTree", Namespace = "http://www.cdiscount.com")]
[XmlArrayItem(ElementName = "headerMessage", Namespace = "http://www.cdiscount.com")]
public List<HeaderMessage> headerMessage get; set;
public class HeaderMessage
[XmlElement(Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")]
public Context Context get; set;
[XmlElement(Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")]
public Localization Localization get; set;
[XmlElement(Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")]
public Security Security get; set;
[XmlElement(Namespace = "http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages")]
public string Version get; set;
public class Context
public int CatalogID get; set;
public int CustomerPoolID get; set;
public int SiteID get; set;
public class Localization
public string CatalogID get; set;
public string Currency get; set;
public int DecimalPosition get; set;
public string Language get; set;
public class Security
public string DomainRightsList get; set;
public string IssuerID get; set;
public string SessionID get; set;
public string SubjectLocality get; set;
public string TokenId get; set;
public string UserName get; set;
【讨论】:
以上是关于将带有命名空间和属性的 XML 转换为 C# 模型类的主要内容,如果未能解决你的问题,请参考以下文章