Solved

Serializing objects C#

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

23 Experts available now in Live!

Get 1:1 Help Now