Out of curiosity, how did you reset the index/position? I've been making a
new MemoryStream out of the old one, and using the new one. In the past,
"Thomas S" wrote:
> Found that the problem had to do with the MemoryStream. After I wrote the
> bytes into it, the index was at the end, so I needed to reset it to the
> beginning before doing the serialization.
>
> T
>
> "Thomas S" <thos37news@gmail.com> wrote in message
> news:e545Fv2sGHA.4080@TK2MSFTNGP03.phx.gbl...
> > Any suggestions on how to deserialize an object from one line of XML?
> >
> > I'm trying to deserialize multiple objects from one XML document, each
> > object on one line of the file. The serialization is working, but when I
> > try to read the line back into a MemoryStream, and then to deserialize
> > from that, I get an error that the root node doesn't exist.
> >
> > Example XML lines:
> >
> > <?xml version="1.0"?><LogItem
> > xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" > > xmlns:xsd="
http://www.w3.org/2001/XMLSchema"><ClientName>DefaultClientName</ClientName></LogItem>
> > <?xml version="1.0"?><LogItem
> > xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" > > xmlns:xsd="
http://www.w3.org/2001/XMLSchema"><ClientName>DefaultClientName2</ClientName></LogItem>
> >
> >
> > I'm using this code to deserialize that:
> >
> > using (StreamReader infile = new StreamReader(lfile))
> > {
> > string inLine = infile.ReadLine();
> > while (inLine != null)
> > {
> > MemoryStream mstrm = new MemoryStream();
> > byte[] writeBytes = Encoding.UTF8.GetBytes(inLine);
> > mstrm.Write(writeBytes, 0, writeBytes.Length);
> >
> > XmlSerializer reader = new XmlSerializer(Type.GetType("LogItem"));
> >
> > XmlReaderSettings xset = new XmlReaderSettings();
> > xset.ConformanceLevel = ConformanceLevel.Document;
> > xset.ValidationType = ValidationType.None;
> > xset.XmlResolver = null;
> > xset.IgnoreWhitespace = true;
> >
> > XmlReader xRead = XmlReader.Create(mstrm, xset);
> >
> > LogItems.Add((LogItem)reader.Deserialize(xRead));
> >
> > inLine = infile.ReadLine();
> > }
> > }
> >
> > I get the following exception:
> >
> > System.InvalidOperationException: There is an error in XML document (0,
> > 0). ---> System.Xml.XmlException: Root element is missing.
> > at System.Xml.XmlTextReaderImpl.Throw(Exception e)
> > at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res)
> > at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
> > at System.Xml.XmlTextReaderImpl.Read()
> > at System.Xml.XmlReader.MoveToContent()
> > at
> > Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderLogItem.Read5_LogItem()
> > --- End of inner exception stack trace ---
> > at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
> > xmlReader, String encodingStyle, XmlDeserializationEvents events)
> > at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
> > xmlReader, String encodingStyle)
> > at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
> > xmlReader)
> >
> > As you can see from the example XML lines, the root element is there. How
> > can I get this to work?
> >
> > T
> >
> >
>
>