将类转换为 XML 到字符串

发布于 2024-12-10 10:56:42 字数 666 浏览 1 评论 0原文

我正在使用 XMLSerializer 将类序列化为 XML。有很多相关示例,并将 XML 保存到文件中。然而,我想要的是将 XML 放入字符串中,而不是将其保存到文件中。

我正在尝试下面的代码,但它不起作用:

public static void Main(string[] args)
        {

            XmlSerializer ser = new XmlSerializer(typeof(TestClass));
            MemoryStream m = new MemoryStream();

            ser.Serialize(m, new TestClass());

            string xml = new StreamReader(m).ReadToEnd();

            Console.WriteLine(xml);

            Console.ReadLine();

        }

        public class TestClass
        {
            public int Legs = 4;
            public int NoOfKills = 100;
        }

关于如何解决这个问题有什么想法吗?

谢谢。

I'm using XMLSerializer to serialize a class into a XML. There are plenty of examples to this and save the XML into a file. However what I want is to put the XML into a string rather than save it to a file.

I'm experimenting with the code below, but it's not working:

public static void Main(string[] args)
        {

            XmlSerializer ser = new XmlSerializer(typeof(TestClass));
            MemoryStream m = new MemoryStream();

            ser.Serialize(m, new TestClass());

            string xml = new StreamReader(m).ReadToEnd();

            Console.WriteLine(xml);

            Console.ReadLine();

        }

        public class TestClass
        {
            public int Legs = 4;
            public int NoOfKills = 100;
        }

Any ideas on how to fix this ?

Thanks.

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

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

发布评论

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

评论(3

雨的味道风的声音 2024-12-17 10:56:42

在读取之前,您必须将内存流放回开头,如下所示:

        XmlSerializer ser = new XmlSerializer(typeof(TestClass));
        MemoryStream m = new MemoryStream();

        ser.Serialize(m, new TestClass());

        // reset to 0 so we start reading from the beginning of the stream
        m.Position = 0;
        string xml = new StreamReader(m).ReadToEnd();

最重要的是,通过调用 dispose 或 close 来关闭资源始终很重要。你的完整代码应该是这样的:

        XmlSerializer ser = new XmlSerializer(typeof(TestClass));
        string xml;

        using (MemoryStream m = new MemoryStream())
        {
            ser.Serialize(m, new TestClass());

            // reset to 0
            m.Position = 0;
            xml = new StreamReader(m).ReadToEnd();
        }

        Console.WriteLine(xml);
        Console.ReadLine();

You have to position your memory stream back to the beginning prior to reading like this:

        XmlSerializer ser = new XmlSerializer(typeof(TestClass));
        MemoryStream m = new MemoryStream();

        ser.Serialize(m, new TestClass());

        // reset to 0 so we start reading from the beginning of the stream
        m.Position = 0;
        string xml = new StreamReader(m).ReadToEnd();

On top of that, it's always important to close resources by either calling dispose or close. Your full code should be something like this:

        XmlSerializer ser = new XmlSerializer(typeof(TestClass));
        string xml;

        using (MemoryStream m = new MemoryStream())
        {
            ser.Serialize(m, new TestClass());

            // reset to 0
            m.Position = 0;
            xml = new StreamReader(m).ReadToEnd();
        }

        Console.WriteLine(xml);
        Console.ReadLine();
讽刺将军 2024-12-17 10:56:42

TestClass 类缺少 [Serializabe] 属性,您必须将内存流的位置设置为开头:

         XmlSerializer ser = new XmlSerializer(typeof(TestClass));
        MemoryStream m = new MemoryStream();
        ser.Serialize(m, new TestClass());
        m.Position = 0;
        string xml = new StreamReader(m).ReadToEnd();
        Console.WriteLine(xml);
        Console.ReadLine();

There's the [Serializabe] attribute missing on class TestClass and you have to set the position of the memory stream to the beginning:

         XmlSerializer ser = new XmlSerializer(typeof(TestClass));
        MemoryStream m = new MemoryStream();
        ser.Serialize(m, new TestClass());
        m.Position = 0;
        string xml = new StreamReader(m).ReadToEnd();
        Console.WriteLine(xml);
        Console.ReadLine();
披肩女神 2024-12-17 10:56:42

您的内存流未关闭,并且位于末尾(下一个可写入的位置)。我的猜测是你必须关闭它,或者寻找它的开头。按照您的方式,您不会阅读任何内容,因为您已经处于流的末尾。因此,在序列化对象之后添加 Seek()。像这样:

        XmlSerializer ser = new XmlSerializer(typeof(TestClass));
        MemoryStream m = new MemoryStream();

        ser.Serialize(m, new TestClass());

        m.Seek(0, SeekOrigin.Begin);   //<-- ADD THIS!

        string xml = new StreamReader(m).ReadToEnd();

        Console.WriteLine(xml);

        Console.ReadLine();

Your memory stream is not closed, and is positioned at the end (next available location to write in). My guess is that you must Close it, or Seek to its beginning. The way you do you don't read anything because you are already at the end of stream. So add Seek() after you serialize objects. Like this:

        XmlSerializer ser = new XmlSerializer(typeof(TestClass));
        MemoryStream m = new MemoryStream();

        ser.Serialize(m, new TestClass());

        m.Seek(0, SeekOrigin.Begin);   //<-- ADD THIS!

        string xml = new StreamReader(m).ReadToEnd();

        Console.WriteLine(xml);

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