XML序列化,编码 [英] XML serialization, encoding

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

问题描述

using System;

public class clsPerson
{
  public  string FirstName;
  public  string MI;
  public  string LastName;
}

class class1
{ 
   static void Main(string[] args)
   {
      clsPerson p=new clsPerson();
      p.FirstName = "Jeff";
      p.MI = "A";
      p.LastName = "Price";
      System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
      x.Serialize(Console.Out, p);
      Console.WriteLine();
      Console.ReadLine();
   }
} 

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

1)

System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());

这是什么行吗?是什么的GetType()?

What does this line do? what is GetType()?

2)我怎么编码为

<?xml version="1.0" encoding="utf-8"?>
< clsPerson xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

而不是

<?xml version="1.0" encoding="IBM437"?>
 <clsPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3
 .org/2001/XMLSchema">

或不包括编码类型呢?

推荐答案

1)的GetType()函数返回一个Type对象重新presenting你的对象的类型,在这种情况下,类 clsPerson 。你也可以使用 typeof运算(clsPerson),并得到了相同的结果。该行针对特定类创建一个XmlSerializer对象。

1) The GetType() function returns a Type object representing the type of your object, in this case the class clsPerson. You could also use typeof(clsPerson) and get the same result. That line creates an XmlSerializer object for your particular class.

2)如果要更改编码,我相信是有序列化()函数,可以让你指定的覆盖。请参见详情MSDN 。您可能需要创建一个XmlWriter对象来虽然使用它,因为这也是在 MSDN细节

2) If you want to change the encoding, I believe there is an override of the Serialize() function that lets you specify that. See MSDN for details. You may have to create an XmlWriter object to use it though, details for that are also on MSDN:

 XmlWriter writer = XmlWriter.Create(Console.Out, settings);

您还可以设置在XmlWriter的编码,该XmlWriterSettings对象具有Encoding属性。

You can also set the encoding in the XmlWriter, the XmlWriterSettings object has an Encoding property.

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

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