Solved

Serialization duplicate tags c#

Posted on 2010-09-21
3
501 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

772 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