Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Serializing objects C#

Posted on 2009-05-19
5
Medium Priority
?
738 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 100 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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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