[英]Convert XML with namespaces and attributes into C# Model class
有人可以幫助我解決以下問題。 我想基於具有命名空間和屬性的 xml 文件創建 ac# 模型類。
這是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>
我不知道如何使用 getter 和 setter 來制作 C# 類模型,用於 xml 標記,例如
s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/
非常感謝您的關注!
嘗試以下:
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; }
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.