Oh yeah... one more thing I forgot to mention was that you could just keep
the XmlSerializer in memory. I'm not completely sure of the context of you
application, but this would solve you problem and would improve performance.
On Mon, Sep 8, 2008 at 9:49 AM, Joseph Irizarry <[EMAIL PROTECTED]>wrote:
> I'm not sure why you're using the StreamWriter with encoding, but that's
> what's messing you up. I tried a few variations of the code below and
> determined that the StreamWriter was the culprit.
> CODE:
>
> [Serializable]
> public class Dog {
> public int Age { get; set; }
> public string Name { get; set; }
> public string Breed { get; set; }
> }
> static void Main(string[] args) {
> var dog = new Dog
> {
> Age = 10,
> Breed = "breed1",
> Name = "Name1"
> };
> for (int i = 0; i < 3; i++) {
> using (var stream =
> File.Open(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
> "\\serial.xml", FileMode.Append)) {
> XmlSerializer xmlS = new XmlSerializer(typeof(Dog));
> xmlS.Serialize(stream, dog);
> }
> }
> }
>
> OUTPUT:
> <?xml version="1.0"?>
> <Dog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="
> http://www.w3.org/2001/XMLSchema">
> <Age>10</Age>
> <Name>Name1</Name>
> <Breed>breed1</Breed>
> </Dog><?xml version="1.0"?>
> <Dog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="
> http://www.w3.org/2001/XMLSchema">
> <Age>10</Age>
> <Name>Name1</Name>
> <Breed>breed1</Breed>
> </Dog><?xml version="1.0"?>
> <Dog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="
> http://www.w3.org/2001/XMLSchema">
> <Age>10</Age>
> <Name>Name1</Name>
> <Breed>breed1</Breed>
> </Dog>
>
>
>
> On Fri, Sep 5, 2008 at 2:59 PM, Dave <[EMAIL PROTECTED]> wrote:
>
>>
>> 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 thread 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 my hostabc</
>> 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>
>>
>> one problem is 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. but this is only
>> 1 issue i'm seeing.
>>
>> 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... (as stated in 2nd paragraph)
>>
>> any help would be most appreciated!
>>
>> thanks, dave
>>
>> >>
>>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web
Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://cm.megasolutions.net/forums/default.aspx
-~----------~----~----~----~------~----~------~--~---