1

I was working on serializing list of object to xml. I got great help fron this link

After some customization I got this code

     private void button1_Click(object sender, EventArgs e)
        {
             PersonalList personen = new PersonalList(); 
            // normal person
            Person normPerson = new Person();
            normPerson.Location = "Pune";
            normPerson.ChangeFrequency = "weekly";
            normPerson.LastModifiedDate = "2014-07-12";
            personen.AddPerson(normPerson);
            Type[] personTypes = { typeof(Person)};
            XmlSerializer serializer = new XmlSerializer(typeof(PersonalList), personTypes); 
            FileStream fs = new FileStream("myxml.xml", FileMode.Create); 
            serializer.Serialize(fs, personen); 
            fs.Close(); 
            personen = null;

            // Deserialize 

        }
    }

[XmlRoot("urlset")]
[XmlInclude(typeof(Person))] // include type class Person
public class PersonalList
{
    [XmlArray("url")]
    [XmlArrayItem("url")]
    public List<Person> Persons = new List<Person>();

    public void AddPerson(Person person)
    {
        Persons.Add(person);
    }
}

[XmlType("Person")] // define Type
public class Person
{

    [XmlElement("loc")]
    public string Location { get; set; }


    [XmlElement("changefreq")]
    public string ChangeFrequency { get; set; }

    [XmlElement("lastmod")]
    public string LastModifiedDate { get; set; }


}

This is producing result as

<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<url>
<url>
<loc>Pune</loc>
<changefreq>weekly</changefreq>
<lastmod>2014-07-12</lastmod>
</url>
</url>
</urlset>

but result I want is

<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <url>
    <loc>Pune</loc>
    <changefreq>weekly</changefreq>
    <lastmod>2014-07-12</lastmod>
    </url>

    </urlset>

I dont want that extra element of ArrayList.

Also I need to have this at root element i.e my defined namespaces for root element.

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

Any Help?

Community
  • 1
  • 1
Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60
  • got the anser thanks to this link http://stackoverflow.com/questions/2006482/c-sharp-xml-serialization-disable-rendering-root-element-of-array. Any solutions to add namespaces to root? – Nitin Varpe Jul 22 '14 at 10:28
  • 1
    Tried: `[XmlRoot(Namespace = "http://foo")]` – jessehouwing Jul 22 '14 at 10:40
  • THanks it worked but how would i add xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" to root – Nitin Varpe Jul 22 '14 at 10:52
  • Best usage of StackOverflow is to add a new question. The docs for namespace overrides and such on the root node can be found here, complete with an example: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmlroot(v=vs.110).aspx – jessehouwing Jul 22 '14 at 13:15

0 Answers0