?
Solved

Serialization duplicate tags c#

Posted on 2010-09-21
3
Medium Priority
?
517 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

800 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