0

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>[email protected]</email>  
>       </contact> 
>       ................................
>       <contact>   
>         <mobile>0555555555</mobile>
>         <home>4212566</home>
>         <office>45698752</office>    
>         <fax>090909</fax>  
>         <email>[email protected]</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

0

2 Answers 2

3

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
}
Sign up to request clarification or add additional context in comments.

Comments

2

To omit XML declaration and default XML namespaces:

var settings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true };

var namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);

using (var writer = XmlWriter.Create(file, settings))
{
    XmlSerializer serializer = new XmlSerializer(source.GetType());
    serializer.Serialize(writer, source, namespaces);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.