20

I'm trying to write to an XML file to the isolated storage but I would like to format it like this:-

<SampleData>
  <Item Property1="AliquaXX" />
  <Item Property1="Integer" />
  <Item Property1="Quisque" />
  <Item Property1="Aenean" />
  <Item Property1="Mauris" />
  <Item Property1="Vivamus" />
  <Item Property1="Nullam" />
  <Item Property1="Nam" />
  <Item Property1="Sed" />
  <Item Property1="Class" />
</SampleData>

but I'm buggered if I can work it out, can anyone help?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
None
  • 203
  • 1
  • 2
  • 5

4 Answers4

27

I suspect you need to create an XmlWriterSettings with the behaviour you want (indentation etc) and then pass that to the XmlWriter on creation. Just setting Indent to true may well be enough:

XmlWriterSettings settings = new XmlWriterSettings { Indent = true };
using (XmlWriter writer = XmlWriter.Create(..., settings))
{
    ...
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
15

You can customize the xml output via the XmlWriterSettings.

You didn't include any code, but you can set the XmlWriterSettings when you create the XmlWriter. You can also just use something like:

var myXmlWriter = new XmlWriterSettings { Indent = true };
Shahin Dohan
  • 6,149
  • 3
  • 41
  • 58
Sam
  • 26,946
  • 12
  • 75
  • 101
  • 5
    this is not possible since XmlWritterSettings.Ident is read-only and doing so throws an exception of type 'System.Xml.XmlException' – Pablo Recalde Dec 02 '15 at 15:50
  • @r1verside looks public to me: https://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.indent(v=vs.110).aspx. It's been awhile since I last answered this, but I'm pretty sure I ran the code myself and it worked fine. Also note that Jon Skeet's answer similarly sets the XmlWriterSetting's Indent property. – Sam Dec 03 '15 at 16:31
  • The Settings property stays null for me. Used `new XmlTextWriter` – MrFox Aug 25 '16 at 15:42
  • 3
    @r1verside Create an XmlWriterSettings before creating the writer, set the desired settings then create an XmlWriter and pass the settings, the XmlWriter accepts an XmlWriterSettings argument. I know this is an old topic but it may help other people. Yes the property Indent for example is public but it throws an exception when trying to change it. – Mihail Georgescu Oct 06 '16 at 19:17
  • This creates an exception: `System.Xml.XmlException: 'The 'XmlWriterSettings.Indent' property is read only and cannot be set.'` – wallyk Apr 10 '18 at 16:02
0

If, like me, you're implementing your own XmlWriter you can do:

var myXmlWriter = new MyXmlWriter(stream, System.Text.Encoding.UTF8)
{
    Formatting = Formatting.Indented
};

or do this.Formatting = Formatting.Indented in it's constructor.

Simon Morgan
  • 2,018
  • 5
  • 23
  • 36
-1

You can use DataSet.GetXML()

Dim column As DataColumn
For Each column In DataSet.Tables.Item(0).Columns
    column.ColumnMapping = MappingType.Attribute
Next
Dim xml As String = DataSet.GetXml()

It is not related to XmlWriter but you can use it for formatting XML.

Göktürk Solmaz
  • 181
  • 1
  • 11