AEMLoviji AEMLoviji - 2 months ago 18
C# Question

C# XML serialization

I want simple XML as :

> <?xml version="1.0" encoding="utf-8" ?>
> <contacts>
> <contact>
> <mobile>0555555555</mobile>
> <home>4212566</home>
> <office>45698752</office>
> <fax>090909</fax>
> <email>sdgdg@dgsdg.com</email>
> </contact>
> ................................
> <contact>
> <mobile>0555555555</mobile>
> <home>4212566</home>
> <office>45698752</office>
> <fax>090909</fax>
> <email>sdgdg@dgsdg.com</email>
> </contact>
> </contacts>


i used sample from link text

all work fine but there has some attributes such as xmlns:xsi and xmlns:xsd.
i dont want to save it on my xml. and dont want to use Replace methods How do it?

I will use it in MVC Application. What is the best way to create an xml on memory?
And look this post link text when going to answer

Answer

Initialize your XmlWriter with an XmlWriterSettings, and set XmlWriterSettings.OmitXmlDeclaration to true:

XmlWriterSettings settings = new XmlWriterSettings { OmitXmlDeclaration = true };
using (XmlWriter writer = XmlWriter.Create(textWriter, settings))
{
    // serialize XML here
}