簡體   English   中英

XML序列化,編碼

[英]XML serialization, encoding

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()?

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">

或者根本不包括編碼類型?

如果您將序列化程序傳遞給XmlWriter,則可以控制一些參數,如編碼,是否省略聲明(例如,片段)等。

這並不是一個明確的指南,而是一個替代方案,以便您可以看到正在發生的事情,以及不僅僅是首先要控制的事情。

另請注意,如果使用StringBuilder而不是MemoryStream創建XmlWriter,則xml將忽略您的編碼並以utf-16編碼形式出現。 有關更多信息,請參閱使用utf8編碼編寫xml的博客文章。

XmlWriterSettings xmlWriterSettings = new XmlWriterSettings 
{ 
    Indent = true, 
    OmitXmlDeclaration = false, 
    Encoding = Encoding.UTF8 
};

using (MemoryStream memoryStream = new MemoryStream() )
using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings))
{   
    var x = new System.Xml.Serialization.XmlSerializer(p.GetType());
    x.Serialize(xmlWriter, p);

    // we just output back to the console for this demo.
    memoryStream.Position = 0; // rewind the stream before reading back.
    using( StreamReader sr = new StreamReader(memoryStream))
    {
        Console.WriteLine(sr.ReadToEnd());
    } // note memory stream disposed by StreamReaders Dispose()
}

1)GetType()函數返回一個Type對象,表示對象的類型,在本例中為類clsPerson 您也可以使用typeof(clsPerson)並獲得相同的結果。 該行為您的特定類創建一個XmlSerializer對象。

2)如果你想改變編碼,我相信有一個Serialize()函數的覆蓋,你可以指定它。 有關詳細信息,請參閱MSDN 您可能必須創建一個XmlWriter對象才能使用它,有關詳細信息也在MSDN上

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

您還可以在XmlWriter中設置編碼,XmlWriterSettings對象具有Encoding屬性。

我把@ robert-paulson提供的解決方案帶到了我試圖做的類似事情並得到了XmlSchema的字符串。 默認情況下,它將返回utf-16。 然而,如上所述,此處的解決方案存在流閉合讀取錯誤。 因此,我利用@Liam提到的tweek移動使用塊來自由地將重構作為擴展方法發布。

    public static string ToXmlString(this XmlSchema xsd)
    {
        var xmlWriterSettings = new XmlWriterSettings
        {
            Indent = true,
            OmitXmlDeclaration = false,
            Encoding = Encoding.UTF8
        };

        using (var memoryStream = new MemoryStream())
        {
            using (var xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings))
            {
                xsd.Write(xmlWriter);
            }

            memoryStream.Position = 0; 
            using (var sr = new StreamReader(memoryStream))
            {
                return sr.ReadToEnd();
            }
        }
    }

1)這為類clsPerson創建了一個XmlSerializer。

2)編碼是IBM437,因為這是Console.Out流的形式。

PS:C#中不喜歡匈牙利符號; 只是命名你的班級人物。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM