[英]deserialize XML document to C# collection of objects
I need some help to point out what I do wrong here. 我需要一些帮助以指出我在这里做错了什么。 I have searched SO and tried different ways to load this XML and I just could not see what would have caused this error: 我已经搜索了SO,并尝试了不同的方式来加载此XML,但我只是看不到是什么原因导致了此错误:
System.InvalidOperationException: There is an error in XML document (1, 1). System.InvalidOperationException:XML文档(1,1)中存在错误。 ---> System.Xml.XmlException: Data at the root level is invalid. ---> System.Xml.XmlException:根级别的数据无效。 Line 1, position 1. 第1行,位置1。
<?xml version="1.0" encoding="utf-8"?>
<folderlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<foldersetting>
<id>1</id>
<source>
<path>\\USApps2\AVL\Attachments</path>
<datelastread>2016-01-25T10:51:12.6030162-08:00</datelastread>
<filter>
<owner>US\</owner>
<filetype>*</filetype>
</filter>
</source>
<destination>
<path>E:\UserData\AVL</path>
<overwrite>false</overwrite>
</destination>
</foldersetting>
<foldersetting>
<id>2</id>
<source>
<path>\\TWAVLSVR\AVL\Attachments</path>
<datelastread>2016-01-22T10:51:12.6030162-08:00</datelastread>
<filter>
<owner>TW\</owner>
<filetype>PDF</filetype>
</filter>
</source>
<destination>
<path>E:\UserData\AVL</path>
<overwrite>false</overwrite>
</destination>
</foldersetting>
<foldersetting>
<id>3</id>
<source>
<path>E:\UserData\AVL</path>
<datelastread>2016-01-22T10:51:12.6030162-08:00</datelastread>
<filter>
<owner>US\</owner>
<filetype>*</filetype>
</filter>
</source>
<destination>
<path>E:\UserData\AVL\Web</path>
<overwrite>false</overwrite>
</destination>
</foldersetting>
</folderlist>
Here is my class definition: 这是我的班级定义:
[Serializable()]
public class Filter
{
[XmlElement("owner")]
public string Owner {get; set;}
[XmlElement("filetype")]
public string FileType { get; set; }
}
[Serializable()]
public class SourceFolder
{
[XmlElement("path")]
public string Path { get; set; }
[XmlElement("datelastread")]
public DateTime DateLastRead { get; set; }
[XmlElement("filter")]
public Filter FilterTypes { get; set; }
}
[Serializable()]
public class DestinationFolder
{
[XmlElement("path")]
public string Path { get; set; }
[XmlElement("overwrite")]
public bool OverWrite { get; set; }
}
[Serializable()]
public class FolderSetting
{
[XmlElement("id")]
public int ID { get; set; }
[XmlElement("source")]
public SourceFolder Source { get; set; }
[XmlElement("destination")]
public DestinationFolder Destination { get; set; }
}
[Serializable()]
[XmlRoot("folderlist")]
public class FolderList
{
public FolderList()
{
FolderSettings = new List<FolderSetting>();
}
[XmlElement("foldersetting")]
public List<FolderSetting> FolderSettings;
}
Here I deserialize the XML: 在这里,我反序列化XML:
XmlSerializer serializer = new XmlSerializer(typeof(FolderList));
using (StringReader reader = new StringReader("C:\\Folders.xml"))
{
FolderList folders = (FolderList)(serializer.Deserialize(reader));
}
I have followed one suggestion on here to populate the classes I have and serialize it to make sure I get the proper format for my classes and it still doesn't help. 我在这里遵循了一个建议,以填充我拥有的类并对其进行序列化,以确保获得适用于我的类的正确格式,但这仍然无济于事。 Hopefully, someone could see what I'm missing here. 希望有人能看到我在这里所缺少的。
The problem is the use of StringReader in your deserialization, you should use StreamReader: 问题是在反序列化中使用StringReader时,应使用StreamReader:
using (StreamReader reader = new StreamReader("D:\\Folders.xml"))
{
FolderList folders = (FolderList)(serializer.Deserialize(reader));
}
So, the result was the the serializer was attempting to deserialize "D:\\Folders.xml", rather than the data file the OP was attempting to deserialize. 因此,结果是序列化程序正在尝试反序列化“ D:\\ Folders.xml”,而不是OP试图反序列化的数据文件。
Your XML is missing the </folderlist> end tag. 您的XML缺少</ folderlist>结束标记。
You can validate your XML here: http://www.xmlvalidation.com/ 您可以在此处验证XML: http : //www.xmlvalidation.com/
As a personal coding style, consider starting your classes with the general and move to the specific. 作为一种个人编码样式,请考虑从一般类开始学习您的类,然后转向特定类。 It's easier to read when you see the top-level object first. 当您首先看到顶层对象时,阅读起来会更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.