根据属性值反序列化 Xml 属性
Deserialize Xml Properties depending from attribute value
我尝试反序列化这个 xml:
<AccountInfo ID="accid1" Currency="EUR">
<AccountNumber international="false">10000</AccountNumber>
<AccountNumber international="true">DE10000</AccountNumber>
<BankCode international="false">22222</BankCode>
<BankCode international="true">BC22222</BankCode>
<AccountHolder>Some Dude</AccountHolder>
</AccountInfo>
进入以下 class:
public class AccountInfo
{
public int ID {get; set;}
public string Currency {get; set;}
public long AccountNumber {get; set;} //this should be filled if international == false
public string BankCode {get; set;}
public long AccountNumberInternational {get; set;} //this should be filled if internation == true
public string BankCodeInternational {get; set;}
public string AccountHolder {get; set;}
}
但我停留在如何告诉反序列化器(System.Xml.Serialization、.NET 4.6.1)根据属性 class 填充 AccountNumber/BankCode 的属性 "international" 来自 AccountNumber/BankCode 来自 xml.
到目前为止,我尝试使用这个 "Serialisation" classes:
[XmlRoot("AccountInfo")]
public class AccountInfo
{
[XmlAttribute]
public int ID { get; set; }
[XmlAttribute]
public string Currency { get; set; }
[XmlElement]
public BankAccount BankAccount { get; set; }
public string AccountHolder { get; set; }
}
public class BankAccount
{
public long AccountNumber { get; set; }
public int BankCode { get; set; }
}
但这甚至没有接近我需要的结构。
我如何声明要序列化的 classes?
XmlSerializer
旨在将数据反序列化为 DTO 模型,该模型 基本上 与 XML 的形状相同,因此类似于:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
static class Program
{
static void Main()
{
var xml = @"<AccountInfo ID=""accid1"" Currency=""EUR"">
<AccountNumber international=""false"">10000</AccountNumber>
<AccountNumber international=""true"">DE10000</AccountNumber>
<BankCode international=""false"">22222</BankCode>
<BankCode international=""true"">BC22222</BankCode>
<AccountHolder>Some Dude</AccountHolder>
</AccountInfo>";
var ser = new XmlSerializer(typeof(AccountInfo));
var obj = ser.Deserialize(new StringReader(xml));
// ...
}
}
public class AccountInfo
{
[XmlElement]
public List<BankAccount> AccountNumber { get; } = new List<BankAccount>();
[XmlElement]
public List<BankAccount> BankCode { get; } = new List<BankAccount>();
public string AccountHolder { get; set; }
[XmlAttribute("ID")]
public string Id {get;set;}
[XmlAttribute]
public string Currency {get;set;}
}
public class BankAccount
{
[XmlAttribute("international")]
public bool International { get; set; }
[XmlText]
public string Number { get; set; }
}
您想要 select 并推入您的 域 模型的哪些值应该由您 之后 完成- 例如,仅 select 列出列表中的国际或非国际项目。
我尝试反序列化这个 xml:
<AccountInfo ID="accid1" Currency="EUR">
<AccountNumber international="false">10000</AccountNumber>
<AccountNumber international="true">DE10000</AccountNumber>
<BankCode international="false">22222</BankCode>
<BankCode international="true">BC22222</BankCode>
<AccountHolder>Some Dude</AccountHolder>
</AccountInfo>
进入以下 class:
public class AccountInfo
{
public int ID {get; set;}
public string Currency {get; set;}
public long AccountNumber {get; set;} //this should be filled if international == false
public string BankCode {get; set;}
public long AccountNumberInternational {get; set;} //this should be filled if internation == true
public string BankCodeInternational {get; set;}
public string AccountHolder {get; set;}
}
但我停留在如何告诉反序列化器(System.Xml.Serialization、.NET 4.6.1)根据属性 class 填充 AccountNumber/BankCode 的属性 "international" 来自 AccountNumber/BankCode 来自 xml.
到目前为止,我尝试使用这个 "Serialisation" classes:
[XmlRoot("AccountInfo")]
public class AccountInfo
{
[XmlAttribute]
public int ID { get; set; }
[XmlAttribute]
public string Currency { get; set; }
[XmlElement]
public BankAccount BankAccount { get; set; }
public string AccountHolder { get; set; }
}
public class BankAccount
{
public long AccountNumber { get; set; }
public int BankCode { get; set; }
}
但这甚至没有接近我需要的结构。
我如何声明要序列化的 classes?
XmlSerializer
旨在将数据反序列化为 DTO 模型,该模型 基本上 与 XML 的形状相同,因此类似于:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
static class Program
{
static void Main()
{
var xml = @"<AccountInfo ID=""accid1"" Currency=""EUR"">
<AccountNumber international=""false"">10000</AccountNumber>
<AccountNumber international=""true"">DE10000</AccountNumber>
<BankCode international=""false"">22222</BankCode>
<BankCode international=""true"">BC22222</BankCode>
<AccountHolder>Some Dude</AccountHolder>
</AccountInfo>";
var ser = new XmlSerializer(typeof(AccountInfo));
var obj = ser.Deserialize(new StringReader(xml));
// ...
}
}
public class AccountInfo
{
[XmlElement]
public List<BankAccount> AccountNumber { get; } = new List<BankAccount>();
[XmlElement]
public List<BankAccount> BankCode { get; } = new List<BankAccount>();
public string AccountHolder { get; set; }
[XmlAttribute("ID")]
public string Id {get;set;}
[XmlAttribute]
public string Currency {get;set;}
}
public class BankAccount
{
[XmlAttribute("international")]
public bool International { get; set; }
[XmlText]
public string Number { get; set; }
}
您想要 select 并推入您的 域 模型的哪些值应该由您 之后 完成- 例如,仅 select 列出列表中的国际或非国际项目。