
July 30th, 2006, 01:05 AM
|
|
|
Deserialize object from one line of 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.XmlS erializationReaderLogItem.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
|

July 30th, 2006, 06:05 AM
|
|
|
Re: Deserialize object from one line of XML
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.comwrote in message
news:e545Fv2sGHA.4080@TK2MSFTNGP03.phx.gbl...
Quote:
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.XmlS erializationReaderLogItem.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
>
>
|
|

August 2nd, 2006, 08:05 PM
|
|
|
Re: Deserialize object from one line of XML
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.
"Thomas S" wrote:
Quote:
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.comwrote in message
news:e545Fv2sGHA.4080@TK2MSFTNGP03.phx.gbl...
Quote:
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.XmlS erializationReaderLogItem.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
|
>
>
>
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|