Error reading XML file

BiggBirdd

New Member
\[code\] Person person = GetPerson(); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add(string.Empty, string.Empty); XmlSerializer serializer = new XmlSerializer(typeof(Person)); string personText = string.Empty; using (MemoryStream memoryStream = new MemoryStream()) { using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings() { Encoding = Encoding.UTF8 })) { serializer.Serialize(xmlWriter, person, ns); xmlWriter.Flush(); personText = Encoding.UTF8.GetString(memoryStream.ToArray()); } } string path = @"D:\person.xml";// Write method 1: File.WriteAllText(path, personText);// Write method 2: using (StreamWriter streamWriter = new StreamWriter(path, false , Encoding.UTF8)) { streamWriter.Write(personText); }// Read the xml using (FileStream fileStream = new FileStream(path, FileMode.Open)) { return XDocument.Load(XmlReader.Create(fileStream)); }\[/code\]When I read the xml after writing using method 2, I get this Data at the root level is invalid. Line 1, position 1. But it works fine using method 1.What is causing this? Any pointers appreciated.
 
Back
Top