 |
| C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 20th, 2009, 12:59 PM
|
|
Authorized User
|
|
Join Date: Feb 2007
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Overwrite the User Input in XML file
Hi
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.
Regards
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);
acnw.Close();
// DeSerialize - Account(Number,Type,Balance)
// Account Number Deserialization
AccountBook acc_Number;
TextReader acnd = newStreamReader(@"c:\Acclist.xml");
acc_Number = (AccountBook)acns.Deserialize(acnd);
acnd.Close();
return created;
|
|

August 20th, 2009, 01:42 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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.
__________________
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the proof.
================================================== =========
|
|
The Following User Says Thank You to gbianchi For This Useful Post:
|
|
|

August 26th, 2009, 11:22 PM
|
|
Authorized User
|
|
Join Date: Feb 2007
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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 !
Regards..
|
|

August 27th, 2009, 06:43 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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.
|
|

August 27th, 2009, 12:43 PM
|
|
Authorized User
|
|
Join Date: Feb 2007
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Thanks for your response, samjudson.
|
|
 |