将Class转换为XML为string

时间:2022-03-16 21:03:02

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.

我正在使用XMLSerializer将类序列化为XML。有很多例子可以将XML保存到文件中。但是我想要的是将XML放入字符串而不是将其保存到文件中。

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.

3 个解决方案

#1


10  

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:

最重要的是,通过调用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();

#2


1  

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

类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();

#3


-4  

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:

您的内存流未关闭,并且位于末尾(下一个可写入的位置)。我的猜测是你必须关闭它,或者寻求它的开始。你做的方式是因为你已经在流的末尾而没有阅读任何内容。因此,在序列化对象后添加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();

#1


10  

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:

最重要的是,通过调用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();

#2


1  

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

类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();

#3


-4  

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:

您的内存流未关闭,并且位于末尾(下一个可写入的位置)。我的猜测是你必须关闭它,或者寻求它的开始。你做的方式是因为你已经在流的末尾而没有阅读任何内容。因此,在序列化对象后添加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();