如何将类序列化为XML [英] How do I Serialize Class in to XML

查看:165
本文介绍了如何将类序列化为XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将类序列化为xml文件。因为我是wpf和mvvm的新手,我发现很多困难。所以任何人都可以用代码和文件名详细解释。





谢谢。

解决方案

序列化与wpf或mvvm无关(我假设你的问题是关于序列化的问题)一般)。以下不是唯一的方法,但如果你想序列化一个对象:



  //  文件:AClass.cs  
使用系统;
使用 System.Collections.Generic;
使用 System.IO;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Xml.Serialization;

命名空间 ConsoleApplication1
{
[Serializable]
公开 AClass
{
public string AProperty { get ; set ; }

public void SerializeMe( string intoFile)
{
// http://msdn.microsoft .com / zh-cn / library / vstudio / system.io.streamwriter
使用 var fs = new StreamWriter(intoFile))
{
// http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
var ser = new XmlSerializer( typeof (AClass));
ser.Serialize(fs, this );
}
}
public static AClass反序列化( string fromFile)
{
AClass instance = null ;
// http://msdn.microsoft.com/en-us/library/vstudio/system。 io.streamreader
使用 var fs = new StreamReader(fromFile))
{
var ser = XmlSerializer( typeof (AClass));
instance =(AClass)ser.Deserialize(fs);
}
返回实例;
}
}
}





用它作为:



  //  文件:Program.cs  
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 ConsoleApplication1
{
class 计划
{
静态 void Main( string [] args)
{
Test( test.xml< /跨度>);
}

静态 void 测试( string xmlFile)
{
AClass a = new AClass(){AProperty = prop value};
a.SerializeMe(xmlFile);

var b = AClass.Deserialize(xmlFile);
}
}
}







更多示例:< a href =http://msdn.microsoft.com/en-us/library/58a18dwa%28v=vs.110%29.aspx> http://msdn.microsoft.com/en-us/library/58a18dwa %28v = vs.110%29.aspx


对象 XML 序列化。不是XML类。



查看下面的XML序列化示例链接。

http://support.microsoft.com/kb/815813



然后使用 XmlDocument 将XML字符串保存到File,如下所示。

  string  exaample =  < xml>< test>< / test>< / xml>; 
XmlDocument xdocument = new XmlDocument();
xdocument.LoadXml(exaample);
xdocument.Save( TestXMLFile.xml);


忘记WPF和MVVM,它们完全无关紧要。接受关注点分离的想法。并了解序列化:

http://en.wikipedia.org/wiki/Serialization [ ^ ],

http://msdn.microsoft.com/en-us/library/7ay27kt9% 28v = vs.110%29.aspx [ ^ ]。



也许最先进和最容易使用的是基于数据合同的序列化

http:/ /msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx [ ^ ]。



在我过去答案,我试着解释为什么会这样所以:

如何在表单应用程序中使用XML文件编写器和阅读器? [ ^ ],

创建属性文件...... [ ^ ]。



-SA

I'm trying to serialize a class to xml file.Since I'm new to wpf and mvvm I find lot of difficulties.So anyone can explain in detail with codes and filenames.


Thanks.

解决方案

serialization is nothing with wpf or mvvm (I assume your question is about serialization in general). The following is not the only way, but if you want to serialize an object:

// File: AClass.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    [Serializable]
    public class AClass
    {
        public string AProperty { get; set; }

        public void SerializeMe(string intoFile)
        {
            // http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamwriter
            using (var fs = new StreamWriter(intoFile))
            {
                // http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
                var ser = new XmlSerializer(typeof(AClass));
                ser.Serialize(fs, this);
            }
        }
        public static AClass Deserialize(string fromFile)
        {
            AClass instance = null;
            // http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamreader
            using (var fs = new StreamReader(fromFile))
            {
                var ser = new XmlSerializer(typeof(AClass));
                instance = (AClass)ser.Deserialize(fs);
            }
            return instance;
        }
    }
}



use it as:

// File: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Test("test.xml");
        }

        static void Test(string xmlFile)
        {
            AClass a = new AClass() { AProperty = "prop value" };
            a.SerializeMe(xmlFile);

            var b = AClass.Deserialize(xmlFile);
        }
    }
}




more example: http://msdn.microsoft.com/en-us/library/58a18dwa%28v=vs.110%29.aspx


Its Object to XML serialization. Not Class to XML.

Have a look at below link for XML Serialization example.
http://support.microsoft.com/kb/815813

And then use XmlDocument to save your XML string to File, as below.

string exaample = "<xml><test></test></xml>";
XmlDocument xdocument = new XmlDocument();
xdocument.LoadXml(exaample);
xdocument.Save("TestXMLFile.xml");


Forget about WPF and MVVM, they are totally irrelevant. Embrace the idea of separation of concerns. And just learn about serialization:
http://en.wikipedia.org/wiki/Serialization[^],
http://msdn.microsoft.com/en-us/library/7ay27kt9%28v=vs.110%29.aspx[^].

Perhaps the most advanced and easiest to use is the serialization based on Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

In my past answers, I tried to explain why is that so:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA


这篇关于如何将类序列化为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆