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 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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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"...
|
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.
|
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...
|
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)...
|
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...
| |
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...
|
by: Hoss |
last post by:
Quick Remark.
I have a class that looks like this
public Class
{
public int Attribu
|
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...
|
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();
...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |