473,498 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 30 '06 #1
2 11512
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" <th********@gmail.comwrote in message
news:e5**************@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.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


Jul 30 '06 #2
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:
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" <th********@gmail.comwrote in message
news:e5**************@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.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


Aug 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4801
by: Alex | last post by:
I am creating an application that allows the user to link a plug-in "utility" class (Watcher) to a class at runtime. There are several Watcher utilities, each with a different style of "watching"...
11
3922
by: Igor | last post by:
Hi. While executing BinaryFormatter.Deserialize() I get: System.InvalidCastException: Specified cast is not valid. I implemented ISerializable interface. What may be a problem? Thanks.
0
2055
by: Fruber Malcome | last post by:
I'm getting a very weird exception and hoping someone may be able to help. I have an Office Add-In that lives in a .dll (for email reference ai.dll) ai.dll makes calls into the core part of the...
4
3292
by: George Addison | last post by:
I understand this might not be the optimal method of deserialization, but how can I deserialize a class to itself? Something like: Public Sub New(Optional ByVal filename as string = Nothing)...
3
4774
by: Jeff Richardson | last post by:
This is a repost from the InfoPath news group. Hi, I am writing a SharePoint application that works with InfoPath forms. When a user submits a completed InfoPath form to a forms library my code...
0
1997
by: PSingh | last post by:
Hi, I know this is a frequently asked question but have tried several combinations and cant seem to figure this out. I am serializing my object as follows: XmlSerializer serializer = new...
1
1863
by: Hoss | last post by:
Quick Remark. I have a class that looks like this public Class { public int Attribu
2
4548
by: conniehl | last post by:
I am able to deserialize a local XML file by passing a textReader object to the xmlSerializer.Deserialize method. My problem Iis that I need to pass a XML file path in form of a URL instead of a...
0
2228
by: connectpalm03-forum | last post by:
I have a class named (MyClassA) in ControlClasses.dll and was able to serialize it to database. Like below SaveTo(MemoryStream stream) { IFormatter formatter = new BinaryFormatter(); ...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.