Results 1 to 3 of 3

Thread: XmlSerialization Questoin

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    XmlSerialization Questoin

    I have a class that contains several string and integer fields. I can serialize this fine using XmlSerializer.
    Now I've added an object array as a field to my class (the object being a user defined class)
    My question is how can i have the parent class serialize the object array as well?

    here's a dummy example. Pretend like i can serialize an instance of the Settings class, except it won't serialize the ItemData[] array inside it. How can i serialize ItemData[] as well

    Code:
        [System.Xml.Serialization.XmlTypeAttribute(Namespace =
        "urn:xmlns:CrReport:settings")]
        [System.Xml.Serialization.XmlRootAttribute("settings",
        Namespace = "urn:xmlns:app:settings", IsNullable = false)]
    Class Settings
    {
    public string filename;
    public int count;
    
    //How can i have it serialize this as well?
    public ItemData[] items;
    }
    
    class ItemData
    {
    public string title
    public int size;
    }
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: XmlSerialization Questoin

    I haven't really delved into the specifics of serialisation too much but I think you'll find that you'd need to at least decorate the ItemData class with the Serializable attribute and, perhaps, the Items property too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: XmlSerialization Questoin

    This worked for me:

    Code:
    [System.Xml.Serialization.XmlTypeAttribute(Namespace =
    "urn:xmlns:CrReport:settings")]
    [System.Xml.Serialization.XmlRootAttribute("settings",
    Namespace = "urn:xmlns:app:settings", IsNullable = false)]
    public class Settings
    {
        public string filename;
        public int count;
    
        //How can i have it serialize this as well?
        public ItemData[] items;
    }
    
    public class ItemData
    {
        public string title;
        public int size;
    }
    
    ... In some test harness...
    
                Settings set = new Settings();
                set.count = 5;
                set.filename = "fds";
                set.items = new ItemData[2];
                set.items[0] = new ItemData();
                set.items[0].size = 3;
                set.items[0].title = "test";
                set.items[1] = new ItemData();
                set.items[1].title = "test2";
    
                string xml = Serialize<Settings>(set);
    
    ... Serialize<T> method: ...
    
    
            private string Serialize<T>(T entity)
            {
                StringBuilder outputXml = new StringBuilder();
    
                XmlSerializer ser = new XmlSerializer(typeof(T));
                using (TextWriter stream = new StringWriter(outputXml))
                {
                    ser.Serialize(stream, entity);
                }
    
                return outputXml.ToString();
            }
    This was the result:

    Code:
    <?xml version="1.0" encoding="utf-16"?>
    <settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:xmlns:app:settings">
      <filename xmlns="urn:xmlns:CrReport:settings">fds</filename>
      <count xmlns="urn:xmlns:CrReport:settings">5</count>
      <items xmlns="urn:xmlns:CrReport:settings">
        <ItemData>
          <title>test</title>
          <size>3</size>
        </ItemData>
        <ItemData>
          <title>test2</title>
          <size>0</size>
        </ItemData>
      </items>
    </settings>
    Last edited by MetalKid; Jul 24th, 2008 at 03:58 PM.
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

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