Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xmlwriter write elements in one line

Tags:

c#

xml

xmlwriter

I tried to save some elements from my application in xml file but when I started to develop it using this code :

public static void WriteInFile(string savefilepath)
        {
            XmlWriter writer = XmlWriter.Create(savefilepath);
            WriteXMLFile(writer);

        }
private static void WriteXMLFile(XmlWriter writer) //Write and Create XML profile for specific type 
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("cmap");
            writer.WriteAttributeString("xmlns", "dcterms",null, "http://purl.org/dc/terms/");
            writer.WriteElementString("xmlns", "http://cmap.ihmc.us/xml/cmap/");
           // writer.WriteAttributeString("xmlns","dc",null, "http://purl.org/dc/elements/1.1/");
            //writer.WriteAttributeString("xmlns", "vcard", null, "http://www.w3.org/2001/vcard-rdf/3.0#");
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
        }

I found that the output in notepad are in one line like this :

<?xml version="1.0" encoding="utf-8"?><cmap
xmlns:dcterms="http://purl.org/dc/terms/"><xmlns>http://cmap.ihmc.us/xml/cmap/</xmlns></cmap>

I want it appear as multiline like this:

<?xml version="1.0" encoding="utf-8"?> <cmap
xmlns:dcterms="http://purl.org/dc/terms/"><xmlns>http://cmap.ihmc.us/xml/cmap/</xmlns>
</cmap>
like image 632
kartal Avatar asked Dec 15 '11 14:12

kartal


3 Answers

You have create an instance of XmlWriterSettings.

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
XmlWriter writer = XmlWriter.Create(savefilepath, settings);
like image 87
KV Prajapati Avatar answered Oct 15 '22 21:10

KV Prajapati


XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (var writer = XmlWriter.Create(savefilepath, settings))
{
     WriteXMLFile(writer);
}
like image 29
Marc Gravell Avatar answered Oct 15 '22 20:10

Marc Gravell


You should use an XmlWriterSettings - set your appropriate formatting options and pass it when creating the XmlWriter.

Read more about it here: http://msdn.microsoft.com/en-us/library/kbef2xz3.aspx

like image 38
zmbq Avatar answered Oct 15 '22 22:10

zmbq