C# 根据属性值反序列化Xml属性
我尝试反序列化此xml:C# 根据属性值反序列化Xml属性,c#,.net,xml-deserialization,C#,.net,Xml Deserialization,我尝试反序列化此xml: <AccountInfo ID="accid1" Currency="EUR"> <AccountNumber international="false">10000</AccountNumber> <AccountNumber international="true">DE10000</AccountNumber> <BankCode international="false">222
<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>
但我一直在讨论如何告诉反序列化程序(System.Xml.Serialization、.NET 4.6.1)根据Xml中AccountNumber/BankCode的属性“international”填充类的属性AccountNumber/BankCode
到目前为止,我尝试使用这种“序列化”类:
[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; }
}
但这甚至不能接近我需要的结构
我需要如何声明用于序列化的类?
XmlSerializer
设计用于将数据反序列化为与XML形状基本相同的DTO模型,因此类似于:
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; }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Xml.Serialization;
静态类程序
{
静态void Main()
{
var xml=@”
10000
DE10000
22222
BC22222
某个家伙
";
var ser=新的XmlSerializer(typeof(AccountInfo));
var obj=ser.Deserialize(新的StringReader(xml));
// ...
}
}
公共类AccountInfo
{
[XmlElement]
public List AccountNumber{get;}=new List();
[XmlElement]
公共列表银行代码{get;}=new List();
公共字符串AccountHolder{get;set;}
[XmlAttribute(“ID”)]
公共字符串Id{get;set;}
[XmlAttribute]
公共字符串货币{get;set;}
}
公营银行帐户
{
[XmlAttribute(“国际”)]
公共布尔国际{get;set;}
[XmlText]
公共字符串编号{get;set;}
}
您希望从中选择哪些值并将其推送到域模型中,这些值应在之后执行,由您—例如,仅从列表中选择国际或非国际项目。您可以检查msdn文档,将XML放入剪贴板,然后选择“特殊粘贴”->“将XML粘贴为类”,Visual Studio将创建一些与XML匹配的类。XML序列化代码可变/可变指向错误schemma/类设计。也许您不应该使用那个XML&类,但应该使用稍微不同的东西(一个名为Account的新基类,具有long属性,以及一个名为internationalcount的子类,该子类继承Account并具有国家字符串)。我不知道这一点。请注意,数组属性上的
[xmlement]
实际上告诉序列化程序XML中的项是扁平的,没有分组。要扩展@caesay notes上的内容-如果没有它,它将是…
(对于BankCode
),列表将获得包装元素谢谢!这个解决方案近乎完美。Id和Currency字段从不填充,“Internation”bool始终为false。有什么想法吗?我很好选择(林克)正确的类之后。。。反序列化将完全符合我的需要。@Kris我已经测试过了;代码工作正常,并按指定加载值。如果它对您的工作方式不同,那么要么您的xml与您所展示的不完全一样(我从问题中复制了它),要么您对我发布的代码做了轻微的更改。要用我答案中的代码测试这一点,只需添加:ser.Serialize(Console.Out,obj)代码>-这将再次显示所有数据,包括@international
、@ID
和@Currency
的正确值。要正确地写入它们,它必须正确地读取它们。@MarcGravel抱歉,我调试了错误的元素:)。非常感谢你!
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; }
}