2011-01-29 82 views
5

我正在嘗試遵循關於XML序列化的microsoft tutorial,但我遇到了一些問題!C#中的Xml序列化#

這是XML文件,用作輸入:

<?xml version="1.0" encoding="utf-8"?> 
<Books xmlns:books="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com"> 
    <money:Book> 
    <books:TITLE>A Book Title</books:TITLE> 
    <money:PRICE books:currency="US Dollar"> 
     <money:price>9.95</money:price> 
    </money:PRICE> 
    </money:Book> 
</Books> 

這是類到XML結合:

public class OrderedItem 
{ 
     [XmlElement(Namespace = "http://www.cpandl.com")] 
     public string ItemName; 
     [XmlElement(Namespace = "http://www.cpandl.com")] 
     public string Description; 
     [XmlElement(Namespace = "http://www.cohowinery.com")] 
     public decimal UnitPrice; 
     [XmlElement(Namespace = "http://www.cpandl.com")] 
     public int Quantity; 
     [XmlElement(Namespace = "http://www.cohowinery.com")] 
     public decimal LineTotal; 
     // A custom method used to calculate price per item. 
     public void Calculate() 
     { 
      LineTotal = UnitPrice * Quantity; 
     } 
    } 

該函數讀取XML入 'OrderedItem' 類:

Console.WriteLine("Reading with Stream"); 

// Create an instance of the XmlSerializer. 
var serializer = new XmlSerializer(typeof(OrderedItem)); 

// Reading the XML document requires a FileStream. 
Stream reader = new FileStream(filename, FileMode.Open); 

// Declare an object variable of the type to be deserialized. 
// Call the Deserialize method to restore the object's state. 
var i = (OrderedItem)serializer.Deserialize(reader); 

Console.SetOut(new StreamWriter("a_output.xml")); 
serializer.Serialize(Console.Out, i); 

這是在讀取和改寫之後的XML:

<?xml version="1.0" encoding="utf-8"?> 
<OrderedItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <ItemName xmlns="http://www.cpandl.com">Widget</ItemName> 
    <Description xmlns="http://www.cpandl.com">Regular Widget</Description> 
    <UnitPrice xmlns="http://www.cohowinery.com">2.3</UnitPrice> 
    <Quantity xmlns="http://www.cpandl.com">10</Quantity> 
    <LineTotal xmlns="http://www.cohowinery.com">23</LineTotal> 
</OrderedItem> 

正如您所看到的,命名空間已展開。我應該如何編寫輸出,以獲得與名稱空間標籤相同的XML而不是uri?

回答

0

您可能要對重載方法看看序列化對象:

default serialization

defining namespaces for serialization

由於沒有提到的,你可以用下面的代碼行定義XmlSerializerNamespaces

// Create an XmlSerializerNamespaces object. 
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 

    // Add two prefix-namespace pairs. 
    ns.Add("inventory", "http://www.cpandl.com"); 
    ns.Add("money", "http://www.cohowinery.com"); 

希望有所幫助。

0

爲了將您的對象序列化爲上面提供的格式,您將不得不在對象上實現IXmlSerializable接口(MSDN Documentation)。這個接口允許你實現一些方法,讓你可以完全控制你的類的序列化結果(還可以將xml反序列化回你的對象)。

這個主題已經在這裏討論爲好,看看這裏的更多信息:Proper way to implement IXmlSerializable

1

您需要添加XmlSerializerNamespaces類型的成員,標有XmlNamespaceDeclarationsAttribute

public class OrderedItem 
{ 
    [XmlNamespaceDeclarations] 
    public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(); 

    ... 
} 

,然後添加序列化時,命名空間聲明:

OrderedItem item = new OrderedItem(); 
item.xmlns.Add("books", "http://www.cpandl.com"); 
item.xmlns.Add("money", "http://www.cohowinery.com"); 
XmlSerializer serializer = new XmlSerializer(typeof(OrderedItem)); 
...