2011-12-16 39 views
0

我有以下類XML序列化問題

[Serializable] 
[XmlRoot("fileset")] 
public class FileSet 
{ 
    [XmlArray("")] 
    [XmlArrayItem(ElementName="file")] 
    public List<File> Files { get; set; } 

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

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

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

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

    public FileSet() 
    { } 

} 

[Serializable] 
[XmlRoot("file")]  
public class File 
{ 
    [XmlElement("action")] 
    public List<Action> Actions { get; set; } 

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

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

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

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

    [XmlAttribute("size")] 
    public long Size { get; set; } 

    [XmlAttribute("creationTime")] 
    public DateTime CreationTime { get; set; } 

    public File() { } 

} 

[Serializable] 
[XmlRoot("action")] 
public class Action 
{ 
    [XmlAttribute("task")] 
    public string Task { get; set; } 

    [XmlAttribute("sequenceid")] 
    public int SequenceId { get; set; } 

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

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

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

    public Action() 
    { } 

} 

我提供的XML是以下類型的

<fileset complete="y" id="NY1109162" search_expression="" format_expression=""> 
    <file type="Datafile" format="NYyymmddn.zip" filefound="y" filename="NY1109162.zip" size="91703" creationTime=""> 
    <actions> 
     <action task="move" sequenceid="1" source="" destination="" desc="move to archive"></action> 
     <action task="copy" sequenceid="2" source="" destination="" desc="copy to production"></action> 
     <action task="unzip" sequenceid="3" source="" destination="" desc="unzip files to working directory"></action> 
    </actions> 
    </file> 
    <file type="Repfile" format="NYyymmddn.rep" filefound="y" filename="NY1109162.rep" size="17" creationTime=""> 
    <actions> 
     <action task="copy" sequenceid="1" source="" destination="" desc="copy to archive"></action> 
     <action task="copy" sequenceid="2" source="" destination="" desc="copy to production"></action> 
    </actions> 
    </file> 
    <file type="Imagefile" format="NYyymmddn_images.zip" filefound="y" filename="NY1109162_images.zip" size="116550" creationTime=""> 
    <actions> 
     <action task="move" sequenceid="1" source="" destination="" desc="move to archive"></action> 
     <action task="copy" sequenceid="2" source="" destination="" desc="copy to production"></action> 
     <action task="unzip" sequenceid="3" source="" destination="" desc="unzip files to working directory"></action> 
    </actions> 
    </file> 
</fileset> 

但是,當我序列化我的文件集對象回到XML,然後將其添加在我不想要的額外標籤上。 序列化輸出是,

<fileset complete="y" id="NY1109162" search_expression="" format_expression=""> 
<Files> 
    <file type="Datafile" format="NYyymmddn.zip" filefound="y" filename="NY1109162.zip" size="91703" creationTime=""> 
    <actions> 
     <action task="move" sequenceid="1" source="" destination="" desc="move to archive"></action> 
     <action task="copy" sequenceid="2" source="" destination="" desc="copy to production"></action> 
     <action task="unzip" sequenceid="3" source="" destination="" desc="unzip files to working directory"></action> 
    </actions> 
    </file> 
    <file type="Repfile" format="NYyymmddn.rep" filefound="y" filename="NY1109162.rep" size="17" creationTime=""> 
    <actions> 
     <action task="copy" sequenceid="1" source="" destination="" desc="copy to archive"></action> 
     <action task="copy" sequenceid="2" source="" destination="" desc="copy to production"></action> 
    </actions> 
    </file> 
    <file type="Imagefile" format="NYyymmddn_images.zip" filefound="y" filename="NY1109162_images.zip" size="116550" creationTime=""> 
    <actions> 
     <action task="move" sequenceid="1" source="" destination="" desc="move to archive"></action> 
     <action task="copy" sequenceid="2" source="" destination="" desc="copy to production"></action> 
     <action task="unzip" sequenceid="3" source="" destination="" desc="unzip files to working directory"></action> 
    </actions> 
    </file> 
</Files> 
</fileset> 

有人能幫忙嗎?

回答

0

我懷疑你可以改變這一點 - 這看起來像預期的行爲,因爲以下 - 解串器應該知道它應該把你的<文件>條目回來。所以它會在你的條目中添加額外的標籤。 想象一下,你將有2個列表類型屬性

[XmlArray("")] 
[XmlArrayItem(ElementName="file")] 
public List<File> Files { get; set; } 

[XmlArray("")] 
[XmlArrayItem(ElementName="file")] 
public List<File> ProcessedFiles { get; set; } 

如果你將被允許連載所有的文件項同級孩子的 - 你將無法反序列化回 - 因爲你無法指定 - 應該放置每個<文件>。

您可以使用手動xml序列化,如果指定,則嚴格要求XML並且不能更改。

+0

是的,絕對正確!我沒有想過這個。現在我已經實現了我自己的xml序列化器。 – Sagar 2011-12-16 09:06:35

0

XmlSerializer不是全部可擴展的。如果你打算使用它,你必須接受你的數據格式的一些妥協。這裏有一些可能的方式來擺脫對數組中間元件的:

  • 更改FileSet類直接覆蓋List<File>的而不是一個單獨的列表屬性。這將擺脫外部元素,但是你失去了指定數組項元素名稱的能力。
  • 在您的FileSet課上實施IXmlSerializable。這很困難,因爲它迫使你手動編寫你自己的序列化和反序列化代碼(這通常是你在使用XmlSerializer時要避免的)。
  • 停止使用XmlSerializer並切換到更靈活的串行器,如DataContractSerializer。這不像XmlSerializer那麼簡單,但它給你更多的自由來指定一個確切的格式。
+0

好吧,我明白了。感謝您的回覆 – Sagar 2011-12-16 09:05:16