Solved

Serialization duplicate tags c#

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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