Solved

Serialization duplicate tags c#

Posted on 2010-09-21
3
508 Views
Last Modified: 2013-12-16
Concept: :
************
I am getting

<Invoice>
  <T>
    <TEST>
      <Title>1</Title>
    </TEST>
    <TEST>
      <Title>2</Title>
    </TEST>
  </T>
  <NAME>NN</NAME>
</Invoice>

And i want :


<Invoice>
    <TEST>
      <Title>1</Title>
    </TEST>
    <TEST>
      <Title>2</Title>
    </TEST>
  <NAME>NN</NAME>
</Invoice>
private void button1_Click(object sender, EventArgs e)
        {
            XMLTEST.TEST T1 = new TEST();
            T1.Title = "1";

            XMLTEST.TEST T2 = new TEST();
            T2.Title = "2";
            
            XmlTest myXML = new XmlTest();
            myXML.NAME = "NN";
            myXML.TEST=new List<TEST> { T1,T2};            
            TextWriter writer = new StreamWriter(@"C:\test.xml");        

            XmlSerializer serializer = new XmlSerializer(typeof(XmlTest));
            serializer.Serialize(writer, myXML);
            writer.Close();            
        }

                  
    }
    [XmlRoot("Invoice")]
    public class XmlTest
    {
        public string NAME
        { get; set; }
         
        public List<TEST> T;

   
      
    }
    public class TEST
    {
        public string Title
        { get; set; }
    }

Open in new window

0
Comment
Question by:WannabeNerd
[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
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
ID: 33724240

Add XmlElement attribute to the List member T like:

[XmlElement("TEST")]
public List<TEST> T;
0
 

Author Comment

by:WannabeNerd
ID: 33724456
Great Thanks for the help.

Only one thing :
I want it make it private so that it is serialized accordingly.

If its public it is on top of file which i want.


eg: NAME element before;

<Invoice>
  <NAME>NN</NAME>
    <TEST>
      <Title>1</Title>
    </TEST>
    <TEST>
      <Title>2</Title>
    </TEST>

</Invoice>
0
 

Author Comment

by:WannabeNerd
ID: 33724523
Never Mind Found it.
Just have to put

[XmlElement("TEST")] on  top :

private List<TEST> _T;


[XmlElement("TEST")]
public List<TEST> T
{
set....
get ....
}

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PowerShell: Adding ToGB to a script 4 69
C# rename a class and update all references efficiently 6 51
SSIS Column mapping 5 40
msiexec won't run 4 21
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 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