.NET 动态序列化转换

发布于 2024-12-17 13:57:09 字数 266 浏览 9 评论 0原文

我有一个节点,其中包含 xml 文件中的分隔值,我将其反序列化(System.Xml.Serialization)为一个类。

这是 XML 数据元素节点的示例:

<Data>1,2,3,4,8,11<Data>

是否可以在反序列化过程中拆分整数数据,以便将数据字符串转换为属性列表,如下所示:

List<int> Data {get; set;}

I have a node that contains delimited values in an xml file which I am deserializing(System.Xml.Serialization) to a class.

This is a sample of the XML data element node:

<Data>1,2,3,4,8,11<Data>

Is it possible to split the integer data during the deserialization process so that the string of data gets converted into a list of property like the following:

List<int> Data {get; set;}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心空情空意 2024-12-24 13:57:09

我建议使用 LINQ to XML 进行序列化,但如果您使用 System.Xml.Serialization,则可以通过对 String 属性使用自定义 setter 来执行此操作,该属性会拆分字符串并填充 List 属性。

List listData {get; set;}
String Data
{
    get
    {
       return String.Empty;
    }
    set
    {
        listData.AddRange(value.Split(','));
    }
}

在 Munim 的编辑之后

在 Minim 添加通用 之后,setter 应该是这样的:

set
{
    string[] temp = value.Split(',');
    foreach (string s in temp)
        listData.Add(Convert.ToInt32(s));
}

I'd recommend using LINQ to XML for serialization, but if you use System.Xml.Serialization you could perform this by using a custom setter for a String property that splits the string and fills the List property.

List listData {get; set;}
String Data
{
    get
    {
       return String.Empty;
    }
    set
    {
        listData.AddRange(value.Split(','));
    }
}

AFTER Munim's EDIT

After Minim added the generic <int>, the setter should be like this:

set
{
    string[] temp = value.Split(',');
    foreach (string s in temp)
        listData.Add(Convert.ToInt32(s));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文