Solved

Serializing objects C#

Posted on 2009-05-19
5
731 Views
Last Modified: 2013-12-17
Hi Experts

Does anyone know how I can change the root node of a serialized object but also keep the class name in the xml produced?

For instance:

[System.Xml.Serialization.XmlRoot("ams")]
    public class litigation
    {        
        public int LenderID;
        public string SolicitorRef;
        public int SolicitorID;
    }

Gives me:

<?xml version="1.0" encoding="utf-8" ?>
<ams xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LenderID>100001</LenderID>
  <SolicitorRef>abc123</SolicitorRef>
  <SolicitorID>1</SolicitorID>
</ams>

I require:

<?xml version="1.0" encoding="utf-8" ?>
<ams xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <litigation>
    <LenderID>100001</LenderID>
    <SolicitorRef>abc123</SolicitorRef>
    <SolicitorID>1</SolicitorID>
  <litigation>
</ams>

Thanks in advance

Mike
0
Comment
Question by:beats_m
  • 3
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 24429362
I believe that if you remove that: [System.Xml.Serialization.XmlRoot("ams")] and replace it by a [Serializable] the root node will have the class name.
0
 

Author Comment

by:beats_m
ID: 24429417
Thanks for your note AlexCode but I require the root node to be ams and also have the class name.

If you look at the xml snippet of what I require. You will see what I mean

Regards
0
 
LVL 30

Assisted Solution

by:Alexandre Simões
Alexandre Simões earned 25 total points
ID: 24429800
You're customizing the default serialization process defined on c#... in your case I thing you need to create your own xml document "by hand".

have a look at these very simple examples:
http://www.csharphelp.com/archives/archive199.html
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=773

Cheers,
Alex
0
 

Accepted Solution

by:
beats_m earned 0 total points
ID: 24430235
Not to worry AlexCode I have solved it.

[Serializable]
    public class ams
    {
        private List<data> _people = new List<data>();
        public ams()
        {
            _people.ForEach(delegate(data _pep)
            {
                _pep = null;
            });
        }    
               
        [XmlElement("litigation")]
        public List<data> litigation
        {
            get { return _people; }
            set { _people = value; }
        }
       
        /// <summary>
        /// The Default property of the Club object
        /// </summary>
        public data this[int index]
        {
            get { return _people[index]; }
            set { _people[index] = value; }
        }
    }
   
    public class data
    {        
        public data()
        {
        }
       
        private int? lenderID;        
        public int? LenderID
        {
            get { return lenderID; }
            set { lenderID = value; }
        }

        private string solicitorRef;
        [System.Xml.Serialization.XmlElement(IsNullable = true)]
        public string SolicitorRef
        {
            get { return solicitorRef; }
            set { solicitorRef = value; }
        }
    }
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 24430404
Glad you solved it.

Cheers!
Alex
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now