2010-07-29 14 views
0

我正在使用System.Xml.Serialization模塊創建一個XML文件。 我有一個類被序列化成一個XML文件。該文件是這樣的:c#使用指定的'encoding'和'stylesheet'行反序列化xml

<itemList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <itemListed> 
    <item ID="81288" Synopsys="Reset search point" CompletedTime="7/27/10 4:12 PM" Resolver="owner1" /> 
    <item ID="81285" Synopsys="Added contructor" CompletedTime="6/05/10 9:23 AM" Resolver="owner2" /> 
    </itemListed> 
</itemList> 

問題是,我想它產生這樣的:

<?xml version="1.0" encoding="iso-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="item.xsl"?> 
    <itemListed> 
    <item ID="81288" Synopsys="Reset search point" CompletedTime="7/27/10 4:12 PM" Resolver="owner1" /> 
    <item ID="81285" Synopsys="Added contructor" CompletedTime="6/05/10 9:23 AM" Resolver="owner2" /> 
    </itemListed> 

任何想法,我需要改變我的課?

我的代碼:

public class Item { [XmlAttribute("ID")] public string ID { get; set; }

[XmlAttribute("Synopsys")] 
    public string Synopsys { get; set; } 

    [XmlAttribute("CompletedTime")] 
    public string CompletedTime { get; set; } 

    [XmlAttribute("Resolver")] 
    public string Resolver { get; set; } 
} 

public class ItemList 
{ 
    [XmlArray(ElementName = "itemListed")] 
    [XmlArrayItem(ElementName = "item")] 
    public List<Item> ItemList { get; set; } 
} 

我感謝所有幫助。 謝謝 託尼

回答

1

我還沒有找到一個簡單的方法來定製樣式表或編碼線,但我發現了一個很好的建議在這裏:

link text

的想法是非常編寫自己的序列化類。我從文章中學到了這個想法,並創建了一個序列化類的類(使用C#庫),然後過濾器修改頭以修改編碼行並添加樣式錶行。
當我加載xml時,我讀取文件,將其傳遞給篩選器以刪除樣式錶行,然後將編碼行更改回來。一旦我完成了,我使用C#提供的反序列化器。 它似乎工作。

託尼

-1

一個解決方案可能是在您的類上實現IXmlSerializable接口。我不確定XmlWriter是否允許你寫一個XML標籤。 Proper way to implement IXmlSerializable?

+0

感謝您的建議,但我認爲,我發現和處理問題更簡單的方法。 Tony – tony 2010-07-30 14:12:18

+0

你可以發佈你的答案是什麼,並將其標記爲答案?我對你想出的東西很感興趣。 – 2010-07-30 17:52:30