CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    object from/to XML

    Hello everyone,


    I want to convert an object instance to XML string, and later read the XML string to construct the object again. Any samples?

    (I find some serialization samples, but all about binary format, I prefer to use XML format.)


    thanks in advance,
    George

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: object from/to XML

    So you want serialization to XML. Could you use LINQ? It would be much simpler. Here is some example: http://mariusbancila.ro/blog/?p=32.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: object from/to XML

    Thanks cilu,


    LINQ is .Net 3.0 feature, right? I have to use version 2.0. So, I can not use LINQ, right?

    Quote Originally Posted by cilu
    So you want serialization to XML. Could you use LINQ? It would be much simpler. Here is some example: http://mariusbancila.ro/blog/?p=32.

    regards,
    George

  4. #4
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: object from/to XML

    1. decorate your class and public properties with the xml serialization attributes
    2. create an xml serializer by passing in the type of your custom class
    3. serialize to a stream
    --
    1. open the stream
    2. create the xml serializer by passing in the type of your custom class
    3. deserialize the stream w/ the serializer to the instance of your class

    Code:
    using System;
    using System.IO;
    using System.Xml.Serialization;
    
    class Program {
        static void Main(string[] args) {
            Foo f = new Foo();
            f.A = "hello world";
            f.B = true;
            f.C = 32;
            XmlSerializer xs = new XmlSerializer(typeof(Foo));
            StringWriter sw = new StringWriter();
            xs.Serialize(sw, f);
            string xml = sw.ToString();
    
            Console.WriteLine(xml);
    
            StringReader sr = new StringReader(xml);
    
            Foo b = xs.Deserialize(sr) as Foo;
    
            Console.ReadLine();
        }
    }
    
    [XmlRoot("foo")]
    public class Foo {
        string a;
        bool b;
        int c;
    
        [XmlAttribute("a")]
        public string A {
            get { return a; }
            set { a = value; }
        }
    
        [XmlElement("b")]
        public bool B {
            get { return b; }
            set { b = value; }
        }
    
        [XmlElement("c")]
        public int C {
            get { return c; }
            set { c = value; }
        }
    
        public Foo() { }
    }

  5. #5
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: object from/to XML

    Cool, MadHatter!


    BTW: LINQ is pronounced as? :-)


    regards,
    George

  6. #6
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: object from/to XML

    MadHatter has shown you a very good example.

    Just to add there are numerous articles on XML Serialization too out there. I am sure you would have landed here, if you would have tried google
    http://www.dotnetjohn.com/articles.aspx?articleid=173
    http://support.microsoft.com/kb/815813

    And MSDN has a whole topic dedicated to Serialization, and I mean both Binary as well as XML.
    Look at Serializing Objects

    PS: I still believe that most of your doubts would get cleared by looking at the relevant documentation on MSDN. You should give it a try.

    Edit --
    LINQ - Langauge Integrated Query

  7. #7
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: object from/to XML

    Thanks Shuja,


    LINQ is pronounced as "LIN CUE"?


    regards,
    George

  8. #8
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: object from/to XML

    pronounced: "link"

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: object from/to XML

    You can also use tools that will create C# classes from an Xsd file. While not perfect, they are a great way to use as a starting point for Xml decorated class generation (as well as a great learning tool).

    Check out Xsd.exe and XsdObjectGen.exe.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: object from/to XML

    Oh yeah, LINQ is pronounced as 'link' - just like one of the characters in the TV series, 'The Mod Squad'.

  11. #11
    Join Date
    Sep 2000
    Location
    Indianapolis
    Posts
    6,758

    Re: object from/to XML

    Quote Originally Posted by George2
    LINQ is .Net 3.0 feature, right? I have to use version 2.0. So, I can not use LINQ, right?
    LINQ is a 3.5 feature, not 3.0.

    Having said that, 3.0 and 3.5 are both simply add-ons to .NET 2.0 more so than "new versions".

    Brad!
    -----------------------------------------------
    Brad! Jones,
    Yowza Publishing
    LotsOfSoftware, LLC

    -----------------------------------------------

  12. #12
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: object from/to XML

    LINQ is a 3.5 feature, not 3.0.
    But it's built on features from 3.0.

    For those interested, here is an explanation with the content of 3.5.
    http://www.danielmoth.com/Blog/2007/...mework-35.html
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  13. #13
    Join Date
    Sep 2000
    Location
    Indianapolis
    Posts
    6,758

    Re: object from/to XML

    Quote Originally Posted by cilu
    But it's built on features from 3.0.

    For those interested, here is an explanation with the content of 3.5.
    http://www.danielmoth.com/Blog/2007/...mework-35.html

    Worded differently --

    If you install .NET 3.0, you will not have LINQ. You need to install .NET 3.5 to have LINQ.

    Brad!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured