【发布时间】:2019-11-04 02:33:43
【问题描述】:
我目前正在使用XMLSerializer以了解它是如何工作的。我能够毫无问题地序列化、保存和反序列化单个对象。但是,当我尝试反序列化多个对象时遇到了问题。我收到此错误:Unhandled exception. System.InvalidOperationException: There is an error in XML document (10, 10).
---> System.Xml.XmlException: Unexpected XML declaration. The XML declaration must be the first node in the document, and no whitespace characters are allowed to appear before it.
我已经尝试过这种方法https://stackoverflow.com/a/16416636/8964654 在这里(我可能做错了)
public static ICollection<T> DeserializeList<T>()
{
string filePath = @"TextFiles/Users.txt";
XmlSerializer serializerTool = new XmlSerializer(typeof(User));
List<T> list = new List<T>();
using (FileStream fs = new FileStream (filePath, FileMode.Open)){
while(fs.Position!=fs.Length)
{
//deserialize each object in the file
var deserialized = (T)serializerTool.Deserialize(fs);
//add individual object to a list
list.Add(deserialized);
}
}
//return the list of objects
return list;
}
没用
这是我的原始代码。我故意调用SaveUser方法两次来模拟方法在不同时间被调用两次
[Serializable]
public class User: ISerializable{
public static void SaveUser(User user){
string filePath = @"TextFiles/Users.txt";
XmlSerializer serializerTool = new XmlSerializer(typeof(User));
using(FileStream fs = new FileStream(filePath, FileMode.Append)){
serializerTool.Serialize(fs, user);
}
}
public static void PrintUser(){
string filePath = @"TextFiles/Users.txt";
XmlSerializer serializerTool = new XmlSerializer(typeof(User));
using (FileStream fs = new FileStream (filePath, FileMode.Open)){
User u1 = (User)serializerTool.Deserialize(fs);
Console.WriteLine($"{u1.FirstName} {u1.LastName}, {u1.DOB.ToShortDateString()}");
}
}
}
class Program
{
static void Main(string[] args)
{
User user1 = new User(){
FirstName = "Kim",
LastName = "Styles",
Address = "500 Penn street, Dallas, 46589",
Username = "KimStyles@yahoo.com",
Password ="Kim2019",
DOB = (new DateTime(1990,10,01)),
Id = 2
};
User user2 = new User(){
FirstName = "Carlos",
LastName = "Santana",
Address = "500 Amigos street,San Jose, California, 46589",
Username = "Carlos.Santana@yahoo.com",
Password ="CarLosSan2019",
DOB = (new DateTime(1990,10,01)),
Id = 2
};
User.SaveUser(user1);
User.SaveUser(user2);
User.PrintUser();
}
}
下面是它如何保存 XML 数据
<?xml version="1.0"?>
<User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FirstName>Kim</FirstName>
<LastName>Styles</LastName>
<DOBProxy>Monday, 01 October 1990</DOBProxy>
<Username>KimStyles@yahoo.com</Username>
<Password>Kim2019</Password>
<Address>500 Penn street, Dallas, 46589</Address>
<Id>1</Id>
</User>
<?xml version="1.0"?>
<User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FirstName>Carlos</FirstName>
<LastName>Santana</LastName>
<DOBProxy>Monday, 01 October 1990</DOBProxy>
<Username>Carlos.Santana@yahoo.com</Username>
<Password>CarLosSan2019</Password>
<Address>500 Amigos street,San Jose, California, 46589</Address>
<Id>2</Id>
</User>
我希望能够检索每个用户的所有数据并打印详细信息。我怎样才能做到这一点?有更好的方法吗?
【问题讨论】:
标签: c# xml serialization deserialization filestream