POLALA.COM
welcome to my space
X
Welcome to:polala.com
Search:  
Web Design | Video Games | RVs | Religion | Management | Supplements and Vitamins | Software | Basketball | Related articles
NAVIGATION - HOME

XML serialization/deserialization difficulties.

Published by: webmaster 2009-01-09
  • 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.
    Serialization for Grown-Ups | replicator.org::
    Aug 13, 2008 I don’t want to discredit XML again, you know, the bloated yet to serialize and deserialize object graphs connected by references.
    http://www.replicator.org/node/74
    HOME
    Problems with Installing Cassini UltiDev with Custom Setup.msi ::
    12 posts - Last post: Sep 22, 2006Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) at System.
    http://www.ultidev.com/Forums/Default.aspx?g=posts&t=128
    HOME

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


    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


  • Never mind, I found an answer.





  • Where's The Advantage In Windows Genuine Advantage?
    Stocks Bounce After S&P Joins Bear Market
    You are looking at:polala.com's XML serialization/deserialization difficulties., click polala.com to home
    #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about XML serialization/deserialization difficulties. , Please add it free.

    About us -Site map -Advertisement -Jion us -Contact usExchange linksSponsor us
    Copyright© 2008 polala.com All Rights Reserved
    Site made&Support support@polala.com    E-mail: web@polala.com