Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hey guys! So I have been working on a project and it's 99% done,all I need to do is add in a Reader for XML.

I have a Serialized Data Class that gets and sets most of my objects in my program. All variables are converted to data.FirstNames etc like so...
C#
private void btnSaveRatings_Click(object sender, EventArgs e)
       {

           //Writes data to an external file.
           SerializedDataClass data = new SerializedDataClass();


           //Gets player information
           data.FirstName = ((Library1.Player)cmbPlayers.SelectedItem).FirstName;
           data.LastName = ((Library1.Player)cmbPlayers.SelectedItem).LastName;

           //Handles squad information.
           data.SquadName = ((Library1.Squad1)cmbSquad.SelectedItem).SquadName;

           //All skills are gathered from data inputs.
           data.SkillName = (cmbSkill.SelectedItem as Library1.Skill).Name;
           data.SkillName2 = (cmbSkill2.SelectedItem as Library1.Skill2).Name;
           data.SkillName3 = (cmbSkill3.SelectedItem as Library1.Skill3).Name;
           data.SkillName4 = (cmbSkill4.SelectedItem as Library1.Skill4).Name;
           data.SkillName5 = (cmbSkill5.SelectedItem as Library1.Skill5).Name;
           data.SkillName6 = (cmbSkill6.SelectedItem as Library1.Skill6).Name;
           data.SkillName7 = (cmbSkill7.SelectedItem as Library1.Skill7).Name;
           data.SkillName8 = (cmbSkill8.SelectedItem as Library1.Skill8).Name;
           data.SkillName9 = (cmbSkill9.SelectedItem as Library1.Skill9).Name;
           data.SkillName10 = (cmbSkill10.SelectedItem as Library1.Skill10).Name;
           data.SkillName11 = (cmbSkill11.SelectedItem as Library1.Skill11).Name;

           //All skill ratings are gathered from data inputs.
           data.StandardRating = (cmbRating1.SelectedItem as Library1.SkillRating).Name;
           data.SpinRating = (cmbRating2.SelectedItem as Library1.SkillRating2).Name;
           data.PopRating = (cmbRating3.SelectedItem as Library1.SkillRating3).Name;
           data.FrontRating = (cmbRating4.SelectedItem as Library1.SkillRating4).Name;
           data.RearRating = (cmbRating5.SelectedItem as Library1.SkillRating5).Name;
           data.SideRating = (cmbRating6.SelectedItem as Library1.SkillRating6).Name;
           data.ScrabbleRating = (cmbRating7.SelectedItem as Library1.SkillRating7).Name;
           data.DropRating = (cmbRating8.SelectedItem as Library1.SkillRating8).Name;
           data.PuntRating = (cmbRating9.SelectedItem as Library1.SkillRating9).Name;
           data.GrubberRating = (cmbRating10.SelectedItem as Library1.SkillRating10).Name;
           data.GoalRating = (cmbRating11.SelectedItem as Library1.SkillRating11).Name;

           //Get information from texbox.
           data.Comment = txtComment1.Text;

           // Create an XmlSerializer to serialize the data to a file
           XmlSerializer xs = new XmlSerializer(typeof(SerializedDataClass));
           using (FileStream fs = new FileStream(data.FirstName + data.LastName + "Data.xml", FileMode.Create))
           {
               xs.Serialize(fs, data);
           }

           //Tells the user that the data has been saved.
           MessageBox.Show(data.FirstName + data.LastName + "'s Data has been saved.", "Success!");
       }

.



This is how my data is saved to XML, using the Pre-fix FirstName variable to differ the XML files from one another.

I have searched all over, but can't seem to find out how to read this file back into my program. Can anyone help me? It's for my project and this step is all that is in the way.

It needs to be able to read all of this information, so that when the program is closed, and re-opened, it reads existing XML files, and keeps the player stats recorded that were entered prior to the program closing.

The players are selected via combo-box, and then the stats are then entered with combo-boxes and converted and serialized.

Hopefully there's a wizz out there that can get this down for me, I'd be very grateful!
Posted
Updated 12-May-15 12:35pm
v2
Comments
Philippe Mori 12-May-15 18:59pm    
Reading is not much different except that the data is transferred in the opposite direction. So what is your problem?

1 solution

Stop suffering from the obsolete and ugly, use something much better, more powerful, consistent, flexible, universal and much easier to use: https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

Please see my past answers:
Creating property files...[^],
How can I utilize XML File streamwriter and reader in my form application?[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900