Click here to Skip to main content
14,695,620 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I generated a XML Serialization using the following C# code
public class Program
    {
        static void Main(string[] args)
        {
            AddressDirectory addr = new AddressDirectory();

            Address A1 = new Address();            
            A1.City = "Chennai";
            A1.Number = 1;
            addr.addressobj.Add(A1);

            Address A2 = new Address();
            A2.City = "Coimbatore";
            A2.Number = 2;
            addr.addressobj.Add(A2);


            Serialize(addr);

        }
        public static void Serialize(AddressDirectory details)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(AddressDirectory));
            using (TextWriter writer = new StreamWriter(@"C:\Users\divakar.m\Documents\Visual Studio 2010\Projects\XmlSerialization2\XmlSer2.xml"))
            {
                serializer.Serialize(writer, details);
            }

        }
       
        public class AddressDirectory
        { 
            public List<Address> addressobj = new List<Address>();
        }
        
        public class Address
        {
            public int Number { get; set; }
            public string City { get; set; }
        }
    }    


The XML it generated was
<?xml version="1.0" encoding="utf-8"?>
<AddressDirectory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <addressobj>
    <Address>
      <Number>1</Number>
      <City>Chennai</City>
    </Address>
    <Address>
      <Number>2</Number>
      <City>Coimbatore</City>
    </Address>
  </addressobj>
</AddressDirectory>


My Question is, how do I remove the "addressobj" tag in the XML from code ?
Posted
Updated 7-Mar-14 3:12am
v4

1 solution

I have changed your code a little bit:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Test_XmlSerialize2
{
    public class Program
    {
        static void Main(string[] args)
        {
            // AddressDirectory addr = new AddressDirectory();

            List<Address> addr = new List<Address>();

            Address A1 = new Address();
            A1.City = "Chennai";
            A1.Number = 1;
            // addr.addressobj.Add(A1);
            addr.Add(A1);

            Address A2 = new Address();
            A2.City = "Coimbatore";
            A2.Number = 2;
            addr.Add(A2);


            Serialize(addr);

        }

        // new version of Serialize
        private static void Serialize(List<Address> addr)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Address>), new XmlRootAttribute("AddressDirectory"));
            using (TextWriter writer = new StreamWriter(@"XmlSer2.xml", false))
            {
                serializer.Serialize(writer, addr);
            }

        }
        // old version
        public static void Serialize(AddressDirectory details)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(AddressDirectory));
            using (TextWriter writer = new StreamWriter(@"XmlSer2.xml", false))
            {
                serializer.Serialize(writer, details);
            }

        }

        // no need to this class
        public class AddressDirectory
        {
            public List<Address> addressobj = new List<Address>();
        }

        public class Address
        {
            public int Number { get; set; }
            public string City { get; set; }
        }
    }
}
   
Comments
Divakar Raj M 12-Mar-14 3:11am
   
@Vedat Ozan Oner: So, is it like if I use a List variable in a separate class, the object name will ALWAYS appear in the XML ?
Vedat Ozan Oner 12-Mar-14 5:09am
   
yes, you can change its name but every node needs a name in a valid xml. moreover, every xml needs a root node. I used the list as root node but altered its name.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900