p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# 2008 aka C# 3.0 (http://p2p.wrox.com/forumdisplay.php?f=354)
-   -   Overwrite the User Input in XML file (http://p2p.wrox.com/showthread.php?t=75810)

shailesh_kumar August 20th, 2009 12:59 PM

Overwrite the User Input in XML file
I'm learning to code in C#. I'm writing the code which gets the UserInput from Command line and saves it in the XML file. I'm using XML serialization and Deserialization to get the User input(for e.g. BankAccountNumber,Type,Balance) in XML file. My code overwrites the last user input and doesn't add/increment the last user input as next entry. I guess, I'm missing some enhancement in my code to save/increment each user input in XML file. I would appreciate, If someone could help me to resolve this issue.

Thanks for Your Help !

Thanks gbianchi and samjudson for your attention and Help ! Please let me know, If you need more information.


Following is the small section of my Code :-

AccNum.accNum = number;
AccNum.accTyp = acc_typ;
AccNum.accBal = acc_balance;

// Serialize - Account(Number,Type,Balance)
// Account Number Serialization
StreamWriter acnw = newStreamWriter(@"c:\Acclist.xml");
XmlSerializer acns = newXmlSerializer(typeof(AccountBook));
acns.Serialize(acnw, AccNum);

// DeSerialize - Account(Number,Type,Balance)
// Account Number Deserialization
AccountBook acc_Number;
TextReader acnd = newStreamReader(@"c:\Acclist.xml");
acc_Number = (

return created;

gbianchi August 20th, 2009 01:42 PM

Well, you are serealizing always on the same file, and only one object..

you need to probably deserialize the file first, has a "collection" of the objects containing there, add the new object (the new account), and serialize again.

shailesh_kumar August 26th, 2009 11:22 PM

The User Input in XML File
Hi Gbianchi,
Thanks for your reply. What I understand from your input is, I have to create a 'new object' each time, I serialize and deserialize a user input. In C#, Can't we have only one object do serialize and deserialize each time a User input is received. I'm sorry, I am not getting a clear picture about this implementation. Could you please elaborate more ?

Thanks for your Help !


samjudson August 27th, 2009 06:43 AM

You are reading a single object to and from the XML file, therefore the file is only ever going to contain 1 object.

You either need to make to 1 object you are reading and writing a collection (e.g. List<AccNum>), or manually manipulate the XML using XmlDocument or some such.

shailesh_kumar August 27th, 2009 12:43 PM

Thanks for your response, samjudson.

All times are GMT -4. The time now is 09:35 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.