Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

c# - Trouble serializing and deserializing multiple objects

I currently playing with the XMLSerializerto understand how it works. I am able to serialize, save and de-serialize a single object without problem. However I run into problems when I try to de-serialize multiple objects. I get this error : 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.

I've tried this approach https://stackoverflow.com/a/16416636/8964654 here (and I could be doing it wrong)


 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;
}

it didn't work

This is my original code. I intentionally call the SaveUser method twice to simulate the method being called twice at different times

 [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 = "[email protected]",
      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 = "[email protected]",
      Password ="CarLosSan2019",
      DOB = (new DateTime(1990,10,01)),
      Id = 2
    };

   User.SaveUser(user1);
   User.SaveUser(user2);
   User.PrintUser();

        }
    }

below is how it saved XML data


<?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>[email protected]</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>[email protected]</Username>
  <Password>CarLosSan2019</Password>
  <Address>500 Amigos street,San Jose, California, 46589</Address>
  <Id>2</Id>
</User>

I want to be able to retrieve all the data and print details of each individual user. How can I do this? Is there a better approach?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your xml has multiple root elements, this is not allowed for valid xml. If you change it to the format, this should work.

<?xml version="1.0"?>
<Users>
   <user></user>
   <user></user>
</Users>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...