Jump to content

C# with xml

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello,

I have a problem with my code and a question, i guess i will start with the questions since it might be the easiest, i am using in my code xmlserializer or using namespace System.Xml.Serialization, wondering, which is more of a best practice or is better for memory etc. xml serializer or linq to xml? also anyone know of a good website that teaches linq to xml? linq to sql etc?

my code is suppose to create an xml file using the method xmltoserialize, which it does, the problem is my other method, it does not deserialize the xml file and add them into the listview or called listview1, here is the code below, i double checked it but its still not working :(

please let me know if you have any questions or what am i doing wrong, thanks :)


public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        List<UserData> userlist = new List<UserData>();


        public void SerializeToXML(List<UserData> info)

        {

            XmlSerializer serializer = new XmlSerializer(typeof(List<UserData>));

            TextWriter textWriter = new StreamWriter(@"C:\userdata.xml");

            serializer.Serialize(textWriter, info);

            textWriter.Close();

        }


        public void DeserializeToXml()

        {

            XmlSerializer deserializer =  new XmlSerializer(typeof(List<UserData>));

            TextReader textReader = new StreamReader(@"C:\userdata.xml");

            List<UserData> userdata = (List<UserData>)deserializer.Deserialize(textReader);

            listView1.Items.Clear();

            foreach (UserData users in userdata)

            {

                ListViewItem lvi = new ListViewItem();

                lvi.Text = users.firstname;

                lvi.SubItems.Add(users.lastname);

                lvi.SubItems.Add(users.occupation);

                listView1.Items.Add(lvi);

            }

            textReader.Close();

        }


        private void btn_submit_Click(object sender, EventArgs e)

        {

            UserData userinfo = new UserData(txt_firstname.Text, txt_lastname.Text, txt_occupation.Text);

            userlist.Add(userinfo);

            listView1.Items.Clear();

            foreach (UserData listusers in userlist)

            {

                ListViewItem lvi = new ListViewItem();

                lvi.Text = listusers.firstname;

                lvi.SubItems.Add(listusers.lastname);

                lvi.SubItems.Add(listusers.occupation);

                listView1.Items.Add(lvi);

            }

            txt_firstname.Clear();

            txt_lastname.Clear();

            txt_occupation.Clear();

            btn_save.Click += new EventHandler(delegate

            {

                SerializeToXML(userlist);


            });

            btn_upload.Click += new EventHandler(delegate

                {

                    DeserializeToXml();

                });

        }



#2
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
            btn_save.Click += new EventHandler(delegate

            {

                SerializeToXML(userlist);


            });

            btn_upload.Click += new EventHandler(delegate

                {

                    DeserializeToXml();

                });


I think this code might be ....a misunderstanding. It's adding event handlers, not invoking them. Try just using those methods alone. Does it work then? ;)



                SerializeToXML(userlist);

                DeserializeToXml();



#3
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Wow... when i put the code into the designated methods as Arek suggested/instructed, the upload worked, WHY??? why cant it work for my anonymous method? weird huh? can anyone tell me why :( also can anyone tell me which would they prefer, doing xml using xmlserializer to read and write a file, or would they use LINQ to XML? which is better or good practice, or it depends on the situation, if so what would be the situation then? same thing for SQL, i am using a namespace using sqlclient to read and write to a database, but would it be good practice for LINQ to SQL using C# or would it just depends, the more info the better please, want to get all info :)

#4
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
You made a serialization code that works like a charm using about 8 lines of code. Nice.

Now make two alternative methods that use LINQ to XML to accomplish the same. The moment you finish this assignment you will have your answer. I can't wait to see you getting back from this trip to hell with your head high and a trophy in your hand. ;)