类的串行化到XML [英] SERIALIZATION OF CLASS TO XML

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

问题描述

你好朋友



请帮我创建一个类,这样在序列化后我会得到一个xml的格式:

< pre lang =HTML> <? xml version = 1.0 encoding = UTF-8 ?>
< 消息 命令 = 点存款 UnitID = 0000 版本 = 1.6 >
< 状态 > GOOD < /状态 >
< 金额 > 200 < / Amount >
< TimeDate > 09/02/2004 08:13:52 AM < / TimeDate >
< 错误 / >
< PlayerName > Doe John < / PlayerName >
< PatronCard > 1900069847 < / PatronCard >
< /消息 >

解决方案

以下是我测试该代码的代码示例。您只需添加所需的属性即可。





 使用 System.IO; 
使用 System.Xml.Serialization;

命名空间 ConsoleApp2912
{
class Program
{
静态 void Main( string [] args)
{
var data = new 信息();
data.Command = 点存款;
data.Version = 1.6;
data.UnitId = 0000;
data.Status = GOOD;
data.Amount = 200 ;


var serializer = new XmlSerializer( typeof (消息));

使用 var strem = new FileStream( d:\\data.xml,FileMode。 OpenOrCreate))
{
serializer.Serialize(strem,data);
}

}
}

[XmlRoot(ElementName = 消息)]
public class 消息
{
[XmlAttribute(AttributeName = 命令)]
public string 命令{ get ; set ; }

[XmlAttribute(AttributeName = UnitID)]
public string UnitId { get ; set ; }

[XmlAttribute(AttributeName = Version)]
public string 版本{ get ; set ; }

[XmlElement(ElementName = 状态)]
public string 状态{ get ; set ; }

[XmlElement(ElementName = 金额)]
public long 金额{ get ; set ; }
}
}


如果您想要最容易同时使用的最全面的解决方案,请不要使用 XmlSeializer ,改为使用数据合约

http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ]。



另请参阅我的我主张这种方法的过去答案:

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

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



-SA


Hello friends

Please help me to create a class so that after serialization i will get a xml of format this:

<?xml version="1.0" encoding="UTF-8" ?>
<Message Command="Point Deposit" UnitID="0000" Version="1.6">
  <Status>GOOD</Status>
  <Amount>200</Amount>
  <TimeDate>09/02/2004 08:13:52 AM</TimeDate>
  <Error />
  <PlayerName>Doe John</PlayerName>
  <PatronCard>1900069847</PatronCard>
</Message>

解决方案

the following is the code sample i tested that code. You just add your required property as you like


using System.IO;
using System.Xml.Serialization;

namespace ConsoleApp2912
{
    class Program
    {
        static void Main(string[] args)
        {
            var data = new Message();
            data.Command = "Point Deposit";
            data.Version = "1.6";
            data.UnitId = "0000";
            data.Status = "GOOD";
            data.Amount = 200;


            var serializer = new XmlSerializer(typeof (Message));

            using (var strem = new FileStream("d:\\data.xml", FileMode.OpenOrCreate))
            {
                serializer.Serialize(strem, data);
            }

        }
    }

    [XmlRoot(ElementName = "Message")]
    public class Message
    {
        [XmlAttribute(AttributeName = "Command")]
        public string Command { get; set; }

        [XmlAttribute(AttributeName = "UnitID")]
        public string UnitId { get; set; }

        [XmlAttribute(AttributeName = "Version")]
        public string Version { get; set; }

        [XmlElement(ElementName = "Status")]
        public string Status { get; set; }

        [XmlElement(ElementName = "Amount")]
        public long Amount { get; set; }
    }
}


If you want the most comprehensive solution which is the easiest to use at the same time, don't use XmlSeializer, use Data Contract instead:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please see also my past answers where I advocate this approach:
Creating property files...[^],
How can I utilize XML File streamwriter and reader in my form application?[^].

—SA


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

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