我如何在c#中反序列化数组数组?(How do I XML deserialize an array of arrays in c#?)

编程入门 行业动态 更新时间:2024-10-05 03:18:29
我如何在c#中反序列化数组数组?(How do I XML deserialize an array of arrays in c#?)

我正在尝试反序列化信用卡箱的对象以在表单上进行品牌验证,但无法正确完成。 内部对象不反序列化,或者主要品牌列表为空。 任何人都可以给我一两手牌吗?

我的XML是这样的:

<?xml version="1.0" encoding="utf-8"?> <Brands> <Brand type="visa"> <Bins> <Bin enabled="true" value="123" /> <Bin enabled="true" value="456" /> <Bin enabled="true" value="789" /> </Bins> </Brand> <Brand type="master"> <Bins> <Bin enabled="true" value="987" /> <Bin enabled="true" value="654" /> <Bin enabled="true" value="321" /> </Bins> </Brand> </Brands>

而我的最新代码(其中brandsCollection为null )是:

[XmlRoot("Brands")] public class CreditCardBrand { [XmlArray("Brands"), XmlArrayItem("Brand")] public CreditCardBrandCollection[] brandsCollection { get; set; } } public class CreditCardBrandCollection { [XmlElement("Bins")] public CreditCardBrandBins[] binsCollection { get; set; } [XmlAttribute("type")] public CreditCardBrands brand { get; set; } } public class CreditCardBrandBins { [XmlAttribute("value")] public string bin { get; set; } [XmlAttribute("enabled")] public bool enabled { get; set; } }

我想将这个xml反序列化为一个Brands数组,每个品牌都有一个属性名称(类型)和一个与它们相关联的bin(只有已启用的)数组,因此我可以在启动时将它放在我的系统内存中。

I am trying to deserialize an object of credit card bins for brand validation on a form, but can't get it done properly. Either the inside object doesn't deserialize, or the main list of brands comes null. Can anyone give me a hand or two?

My XML is like this:

<?xml version="1.0" encoding="utf-8"?> <Brands> <Brand type="visa"> <Bins> <Bin enabled="true" value="123" /> <Bin enabled="true" value="456" /> <Bin enabled="true" value="789" /> </Bins> </Brand> <Brand type="master"> <Bins> <Bin enabled="true" value="987" /> <Bin enabled="true" value="654" /> <Bin enabled="true" value="321" /> </Bins> </Brand> </Brands>

and my latest code(which brings brandsCollection null) is:

[XmlRoot("Brands")] public class CreditCardBrand { [XmlArray("Brands"), XmlArrayItem("Brand")] public CreditCardBrandCollection[] brandsCollection { get; set; } } public class CreditCardBrandCollection { [XmlElement("Bins")] public CreditCardBrandBins[] binsCollection { get; set; } [XmlAttribute("type")] public CreditCardBrands brand { get; set; } } public class CreditCardBrandBins { [XmlAttribute("value")] public string bin { get; set; } [XmlAttribute("enabled")] public bool enabled { get; set; } }

I want to deserialize this xml into an array of Brands, each brand having a property name(type) and an array of bins(only the enabled ones) associated to them, so I can put it in memory at my system on startup.

最满意答案

如果你想使用Linq2Xml

XDocument xDoc = XDocument.Parse(xml); //or XDocument.Load(filename) List<CreditCardBrand> brands = xDoc.Descendants("Brand") .Select(br => new CreditCardBrand() { Type = br.Attribute("type").Value, Bins = br.Descendants("Bin") .Select(b => new CreditCardBin(){ Enabled = (bool)b.Attribute("enabled"), Value = b.Attribute("value").Value, }).Where(b => b.Enabled == true) .ToList() }) .ToList();

-

public class CreditCardBrand { public string Type { get; set; } public List<CreditCardBin> Bins { get; set; } } public class CreditCardBin { public string Value { get; set; } public bool Enabled { get; set; } }

If you want to use Linq2Xml

XDocument xDoc = XDocument.Parse(xml); //or XDocument.Load(filename) List<CreditCardBrand> brands = xDoc.Descendants("Brand") .Select(br => new CreditCardBrand() { Type = br.Attribute("type").Value, Bins = br.Descendants("Bin") .Select(b => new CreditCardBin(){ Enabled = (bool)b.Attribute("enabled"), Value = b.Attribute("value").Value, }).Where(b => b.Enabled == true) .ToList() }) .ToList();

--

public class CreditCardBrand { public string Type { get; set; } public List<CreditCardBin> Bins { get; set; } } public class CreditCardBin { public string Value { get; set; } public bool Enabled { get; set; } }

更多推荐

本文发布于:2023-08-07 11:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464084.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   序列化   如何在   XML   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!