XmlSerializer usage

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

All the examples i've found using XmlSerializer work with one time
serialization / deserialization of objects... or one time
serialization / deserialization of collection of objects.

I need to be able to keep a file stream open, and just serialize
(IrcMessage) objects as they come into my server, then eventually have
some other process deserialize the accumulated (IrcMessage) objects.
the problem is, every time i call:

StreamWriter messageWriter = new
StreamWriter(_logFileStream, Encoding.ASCII);
XmlSerializer writer = new
XmlSerializer(typeof(IrcMessage));
writer.Serialize(messageWriter, ircMessage2);

where:

[Serializable]
public class IrcMessage
{
int _channelId = -1;
public int ChannelId
{
get { return _channelId; }
set { _channelId = value; }
}

string _ircMessageText = null;
public string IrcMessageText
{
get { return _ircMessageText; }
set { _ircMessageText = value; }
}
}

it causes the following output:

<?xml version="1.0" encoding="us-ascii"?>
<IrcMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ChannelId>1</ChannelId>
<IrcMessageText>103 Host irc.freenode.net resolves to:
154.35.200.44, 140.211.166.3, 140.211.166.4, 204.11.244.21,
209.177.146.34, 216.155.130.130, 213.92.8.4, 207.158.1.150,
216.165.191.52, 89.16.176.16, 64.161.254.20, 82.96.64.4</
IrcMessageText>
</IrcMessage><?xml version="1.0" encoding="us-ascii"?>
<IrcMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ChannelId>2</ChannelId>
<IrcMessageText>boohaha string</IrcMessageText>
</IrcMessage><?xml version="1.0" encoding="us-ascii"?>
<IrcMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ChannelId>1</ChannelId>
<IrcMessageText>sasdjlfsdjfsdjsfdjkfsdjksdfjksdafjk;sd;jasdf</
IrcMessageText>
</IrcMessage>

i don't want the xml version info to keep being rewritten...

when trying to deserialize:

StreamReader ircMessageReader = new
StreamReader(logFileStream);
XmlSerializer ircMessageParser = new
XmlSerializer(typeof(IrcMessage));
return
(IrcMessage)ircMessageParser.Deserialize(ircMessageReader);

i blow up with Unexpected XML declaration... trying to manually remove
the declaration from the files causes other errors.

so i'm stuck. let me know if any further clarification is needed.

thanks, dave
 
Dave said:
All the examples i've found using XmlSerializer work with one time
serialization / deserialization of objects... or one time
serialization / deserialization of collection of objects.

I need to be able to keep a file stream open, and just serialize
(IrcMessage) objects as they come into my server, then eventually have
some other process deserialize the accumulated (IrcMessage) objects.
the problem is, every time i call:
...
when trying to deserialize:

StreamReader ircMessageReader = new
StreamReader(logFileStream);
XmlSerializer ircMessageParser = new
XmlSerializer(typeof(IrcMessage));
return
(IrcMessage)ircMessageParser.Deserialize(ircMessageReader);

i blow up with Unexpected XML declaration... trying to manually remove
the declaration from the files causes other errors.

so i'm stuck. let me know if any further clarification is needed.


(copy/pasted from a response I made on CodeProject.com)

Attached to the serialized data is the name/version of
the assembly that defines the type of the serialized data.
The system attempts to use that same assembly to get the
type info required to deserialize the data into an object
of the same type.

You can make the same assembly available on both ends
or use a SerializationBinder on the deserializing end.

Mark
 
(copy/pasted from a response I made on CodeProject.com)

Attached to the serialized data is the name/version of
the assembly that defines the type of the serialized data.
The system attempts to use that same assembly to get the
type info required to deserialize the data into an object
of the same type.

You can make the same assembly available on both ends
or use a SerializationBinder on the deserializing end.

Mark

thanks for your reply. i'm not sure SerializationBinder is what i'm
after though - as playing with it i still see versioning info coming
out. (this object also seems much hard to use) also my main problem
is not as simple as unwanted versioning info.

reiterating my 2nd paragraph:
i need to be able to keep a file stream open, and just serialize a
sequence of (IrcMessage) objects AS they come into my server, then
eventually have some other process deserialize the accumulated
(IrcMessage) objects.

i could accumulate the objects in a collection then do a onetime
serialization of a collection of IrcMessage objects, then deserialize
the collection in the same manner - but this IS NOT what i'm after.
I.e. i wish to serialize IrcMessage objects to file as they come in...
 
Dave said:
All the examples i've found using XmlSerializer work with one time
serialization / deserialization of objects... or one time
serialization / deserialization of collection of objects.

I need to be able to keep a file stream open, and just serialize
(IrcMessage) objects as they come into my server, then eventually have
some other process deserialize the accumulated (IrcMessage) objects.
the problem is, every time i call:

StreamWriter messageWriter = new
StreamWriter(_logFileStream, Encoding.ASCII);
XmlSerializer writer = new
XmlSerializer(typeof(IrcMessage));
writer.Serialize(messageWriter, ircMessage2); ....

it causes the following output:

<?xml version="1.0" encoding="us-ascii"?>
....
|> i don't want the xml version info to keep being rewritten...

You should create an XmlWriter on top of that StreamWriter, and for its
XmlWriterSettings, set OmitXmlDeclaration=true.
Come to think of it, maybe it's a good idea to also set
ConformanceLevel=ConformanceLevel.Fragment.
 
Back
Top