XML serialization/deserialization difficulties

EDIT: Should have mentioned that this was a C# related question in the title, Sorry.

Here I’ve got some code that serializes an object (obj) by converting it to XML, deserializes it back to an object (obj2), and then serializes it again. Unfortunately there are a couple of problems. The first is that I can’t figure out how to change the values of the XML while the object is serialized, so that the new deserialized object (when it is created) reflects these new values. The part where I’ve attempted to do this generates an error and is commented out ( // f.InnerText = “New from Xml”;). The second problem is that I would simply like the code to be more dynamic and economical. For starters, I feel I ought to be using the same XmlSerializer to perform the first and all subsequent serialization/deserializations, perhaps the same StringBuilder, StringWriter, XmlDocument, etc., but each time I fiddle I get all sorts of errors.

Sorry for my ignorance in these matters, I am new to C#. Any help would be appreciated. :slight_smile:


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            MenuLink obj = new MenuLink();

            obj.Title = "Some Title";
            obj.ToolTip = "Some ToolTip";
            obj.Url = "Some Url";
            obj.IsExternal = false;

            //Serialize (convert an object instance to an XML document):
            XmlSerializer ser = new XmlSerializer(obj.GetType());
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            System.IO.StringWriter writer = new System.IO.StringWriter(sb);
            ser.Serialize(writer, obj);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(sb.ToString());
            Console.WriteLine(sb.ToString());

            // Change some values of the Xml...

            Console.WriteLine(doc.GetElementsByTagName("ToolTip"));
            XmlNodeList nl = doc.GetElementsByTagName("ToolTip");
            foreach (XmlElement e in nl)
            {
                foreach (XmlElement f in e.ParentNode)
                {
                    Console.WriteLine(f.Name + ": " + f.InnerText);
                   // f.InnerText = "New from Xml";
                }
            }
  
            //Deserialize (convert an XML document into an object instance):
             
            XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
            XmlSerializer deser = new XmlSerializer(typeof(MenuLink));
            object objTemp = deser.Deserialize(reader);
            MenuLink obj2 = (MenuLink)objTemp;

            // Serialize it again...

            XmlSerializer ser2 = new XmlSerializer(obj.GetType());
            System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
            System.IO.StringWriter writer2 = new System.IO.StringWriter(sb2);
            ser2.Serialize(writer2, obj2);
            XmlDocument doc2 = new XmlDocument();
            doc2.LoadXml(sb2.ToString());
            Console.WriteLine(sb2.ToString());
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication1
{

    public class MenuLink
    {

        private string _Url, _Title, _ToolTip;

        private bool _IsExternal;


        public MenuLink()
        {

            _Url = "";

            _Title = "";

            _ToolTip = "";

            _IsExternal = false;

        }

        public string Url
        {

            get { return _Url; }

            set { _Url = value; }

        }

        public string Title
        {

            get { return _Title; }

            set { _Title = value; }

        }

        public string ToolTip
        {

            get { return _ToolTip; }

            set { _ToolTip = value; }

        }

        public bool IsExternal
        {

            get { return _IsExternal; }

            set { _IsExternal = value; }
        }
    }
}

Fingers