Solved

Serializing objects C#

Posted on 2009-05-19
5
737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

627 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