c# - 根据属性值反序列化 Xml 属性

标签 c# .net xml-deserialization

我尝试反序列化这个 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)根据来自 AccountNumber/BankCode 的属性“international”填充类的属性 AccountNumber/BankCode XML.

到目前为止,我尝试使用这个“序列化”类:

    [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; }
    }

但这甚至没有接近我需要的结构。

Completely wrong xml

我需要如何声明序列化类?

最佳答案

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; }
}

您要从中选择哪些值并将其推送到您的模型中,您应该之后完成 - 例如仅选择国际或非国际列表中的项目。

关于c# - 根据属性值反序列化 Xml 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48166756/

相关文章:

.net - 更改文本框输入

c# - 在 mstest 期间创建文件 - System.UnauthorizedAccessException

c# - 输入字符串的格式不正确

c# - 在异步任务中使用 HttpContext

c# - 我可以在内部制作 ASP.NET Core Controller 吗?

C# 将 linq 列表 (int) 项更改为 (string)

c# - 使用未包装的集合反序列化 XML 时遇到问题

java - 简单 XML - 如何从内部元素开始获取数据?

c# - 如何使用 Ant 和 Mono 编译和构建 C# 项目?

c# - 从 Python 或其他语言中使用 MassTransit