Solved

Serializing objects C#

Posted on 2009-05-19
5
736 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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