我有以下课程
[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>
但是当我将我的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 :(得分:0)
我怀疑你可以改变这个 - 这看起来像预期的行为,因为以下 - 反序列化器应该知道它应该放在你的&lt; file&gt;退回。因此它会在您的条目周围添加其他标记。 想象一下,你将有2个列表类型属性
[XmlArray("")]
[XmlArrayItem(ElementName="file")]
public List<File> Files { get; set; }
[XmlArray("")]
[XmlArrayItem(ElementName="file")]
public List<File> ProcessedFiles { get; set; }
如果允许您将所有文件条目序列化到相同级别的子级别 - 您将无法将其反序列化 - 因为您无法指定 - 其中每个&lt; file&gt;应该放。
如果严格要求指定XML且无法更改,则可以使用手动xml序列化。
答案 1 :(得分:0)
XmlSerializer
并非完全可扩展。如果您要使用它,则必须接受数据格式的一些妥协。以下是一些可能的方法来摆脱阵列的干预元素:
FileSet
类更改为直接覆盖List<File>
,而不是使用单独的列表属性。这将删除外部元素,但您无法指定数组项元素的名称。IXmlSerializable
课程上实施FileSet
。这很困难,因为它会强制您手动编写自己的序列化和反序列化代码(这通常是您在使用XmlSerializer
时要避免的)。XmlSerializer
并切换到更灵活的序列化程序,例如DataContractSerializer
。这不像XmlSerializer
那么简单,但它可以让您更自由地指定确切的格式。