all groups > dotnet xml > july 2006 >
You're in the

dotnet xml

group:

Deserialize object from one line of XML


Deserialize object from one line of XML Thomas S
7/29/2006 5:04:50 PM
dotnet xml:
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

Re: Deserialize object from one line of XML Thomas S
7/29/2006 10:03:37 PM
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

[quoted text, click to view]

Re: Deserialize object from one line of XML Jonathon J. Howey
8/2/2006 12:06:40 PM
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,
i've tried (stream.Position = 0) and (stream.Seek( 0, SeekOrigin.Begin ) )
and get an exception about accessing a closed stream.

Thanks.

[quoted text, click to view]
AddThis Social Bookmark Button