我一直在尝试将xml文档反序列化为类。我以前做过这个,但没有这么多的嵌套。我正在尝试复制一个简单的目录树。我遇到的问题是文件方面。目录部分工作,我可以到达文件,但反序列化文件名到我的类是不完全匹配。我的XMl文档是:
<?xml version="1.0" encoding="utf-8"?>
<Directories>
<Directory Path="Path1">
<Files>
<File Type="None">File1</File>
<File Type="None">File2</File>
<File Type="EXE">File3</File>
</Files>
<Files>
<File Type="DLL">File4</File>
<File Type="None">File5</File>
<File Type="EXE">File6</File>
</Files>
<Files>
<File Type="DLL">File7</File>
<File Type="None">File8</File>
<File Type="EXE">File9</File>
</Files>
</Directory>
<Directory Path="Path2">
<Files>
<File Type="DLL">File10</File>
<File Type="None">File11</File>
<File Type="EXE">File12</File>
</Files>
<Files>
<File Type="DLL">File13</File>
<File Type="None">File14</File>
<File Type="EXE">File15</File>
</Files>
<Files>
<File Type="DLL">File16</File>
<File Type="None">File17</File>
<File Type="EXE">File18</File>
</Files>
</Directory>
<Directory Path="Path3">
<Files>
<File Type="DLL">File19</File>
<File Type="None">File20</File>
<File Type="EXE">File21</File>
</Files>
<Files>
<File Type="DLL">File22</File>
<File Type="None">File23</File>
<File Type="EXE">File24</File>
</Files>
<Files>
<File Type="DLL">File25</File>
<File Type="None">File26</File>
<File Type="EXE">File27</File>
</Files>
</Directory>
</Directories>
这是我的班级:
[XmlRoot("Directories")]
public class Directories
{
[XmlElement("Directory")]
public Directory[] Directory { get; set; }
}
public class Directory
{
[XmlAttribute("Path")]
public string Path { get; set; }
[XmlArray("Files")]
[XmlArrayItem("File")]//this causes the object to structure properly, but doesn't populate the string file property in the class with or without the attribute decoration
public XmlFileInfo[] Files { get; set; }
}
public class XmlFileInfo
{
[XmlElement("File")]
public string File { get; set; }
[XmlAttribute("Type")]//this works fine if this class is referenced as an array
public /*External Enum*/ FileTypes Type { get; set; }
}
上面的带有装饰的类正确填充了类型枚举但没有文件字符串。带有装饰的以下类使用文件树中的第一个文件填充文件字符串,并且未正确填充类型枚举
[XmlRoot("Directories")]
public class Directories
{
[XmlElement("Directory")]
public Directory[] Directory { get; set; }
}
public class Directory
{
[XmlAttribute("Path")]
public string Path { get; set; }
[XmlElement("Files")]
//[XmlArrayItem("File")]
public XmlFileInfo[] Files { get; set; }
}
public class XmlFileInfo
{
[XmlElement("File")]
public string File { get; set; }
[XmlAttribute("Type")]
public SignatureChecker_Mtabs.Program.FileTypes Type { get; set; }
}
我缺少什么?
答案 0 :(得分:0)
我重新上课了。我的问题是我的xmlFileInfo的属性。我设法不正确,因为时间限制无法纠正这个问题。因此,我删除属性并使用此类
[Serializable]
//[XmlRoot("Directories")]
public class Directories
{
//[XmlElement("Directory")]
public List<Directory> Directory { get; set; }
}
[Serializable]
public class Directory
{
//[XmlAttribute()]
public string Path { get; set; }
//[XmlArray("Files")]
//[XmlArrayItem("File")]
public List<XmlFileInfo> Files { get; set; }
}
[Serializable]
public class XmlFileInfo
{
//[XmlElement("File")]
public string File { get; set; }
//[XmlAttribute()]
public MyobstruficatedEnum Type { get; set; }
}