C#:包含泛型列表的类的 XML 序列化

发布于 2024-12-13 17:26:55 字数 918 浏览 0 评论 0原文

我想知道是否有任何方法可以将包含泛型列表的类序列化为 xml? 它看起来像这样:

class Program
{
    static void Main(string[] args)
    {
        var o = new ContainerClass();
        o.Values = new List<SomeClass> {new SomeClass<int>(), new SomeClass<long>()};

        XmlSerializer xs = new XmlSerializer(typeof(ContainerClass));
        MemoryStream buffer = new MemoryStream();
        using (TextWriter writer = new StreamWriter(buffer))
        {
            xs.Serialize(writer, o); // InvalidOperationException here
        }
        var xml = Encoding.UTF8.GetString(buffer.ToArray());
    }
}

public class ContainerClass
{
    public List<SomeClass> Values { get; set; }
}

public class SomeClass
{
}

public class SomeClass<T> : SomeClass
{
}

我知道有一种方法可以将额外的类型传递给序列化器,但现在没有办法传递可能出现的每种组合。

我尝试以各种方式实现 IXmlSerialized 但没有成功。

知道如何处理吗?

如果有任何可以处理它的外部库,我也开放。

I wonder if there is any way to serialize to xml a class that contains list of generics?
It looks like this:

class Program
{
    static void Main(string[] args)
    {
        var o = new ContainerClass();
        o.Values = new List<SomeClass> {new SomeClass<int>(), new SomeClass<long>()};

        XmlSerializer xs = new XmlSerializer(typeof(ContainerClass));
        MemoryStream buffer = new MemoryStream();
        using (TextWriter writer = new StreamWriter(buffer))
        {
            xs.Serialize(writer, o); // InvalidOperationException here
        }
        var xml = Encoding.UTF8.GetString(buffer.ToArray());
    }
}

public class ContainerClass
{
    public List<SomeClass> Values { get; set; }
}

public class SomeClass
{
}

public class SomeClass<T> : SomeClass
{
}

I know that there is a way to pass extra types to the serializer, but there is no way to now every combination that could probably appear.

I've tried to implement IXmlSerializable i various ways without success.

Any idea how to deal with it?

I am also open for external libraries if there is any that can handle it.

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

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

发布评论

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

评论(2

何以心动 2024-12-20 17:26:55

我发现 YAXLib 可以完成这项工作。

I've found YAXLib which does the job.

美男兮 2024-12-20 17:26:55

在尝试了几个库之后,我决定编写自己的代码:

Atlas Xml Serializer

获取 binaries 文件夹下的 dll 并将其添加到您的项目中。然后您的代码应该像这样工作:

class Program
{
    static void Main(string[] args)
    {
        var o = new ContainerClass();
        o.Values = new List<SomeClass> {new SomeClass<int>(), new SomeClass<long>()};

        var xml = Atlas.Xml.Serializer.Serialize(o, true);
    }
}

public class ContainerClass
{
    public List<SomeClass> Values { get; set; }
}

public class SomeClass
{
}

public class SomeClass<T> : SomeClass
{
}

输出将类似于:

<ContainerClass>
    <Values>
        <item _type="WinformsTestApp.SomeClass`1[[System.Int32]], WinformsTestApp" />
        <item _type="WinformsTestApp.SomeClass`1[[System.Int64]], WinformsTestApp" />
    </Values>
</ContainerClass>

PS:WinformsTestApp 是我的测试应用程序名称,因此它将更改以反映您的程序集名称。

After trying few libraries, I decided to code my own:

Atlas Xml Serializer

Get the dll under binaries folder and add it to your project. Then your code should work like this:

class Program
{
    static void Main(string[] args)
    {
        var o = new ContainerClass();
        o.Values = new List<SomeClass> {new SomeClass<int>(), new SomeClass<long>()};

        var xml = Atlas.Xml.Serializer.Serialize(o, true);
    }
}

public class ContainerClass
{
    public List<SomeClass> Values { get; set; }
}

public class SomeClass
{
}

public class SomeClass<T> : SomeClass
{
}

Output will be similar to this:

<ContainerClass>
    <Values>
        <item _type="WinformsTestApp.SomeClass`1[[System.Int32]], WinformsTestApp" />
        <item _type="WinformsTestApp.SomeClass`1[[System.Int64]], WinformsTestApp" />
    </Values>
</ContainerClass>

PS: WinformsTestApp is my test appilcations name, so it'll change to reflect your assembly name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文