Writing formatted XML with XmlWriter
Asked Answered
P

4

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?

Passageway answered 23/9, 2011 at 21:57 Comment(3)
What code do you have and what is its output?Samiel
curious why you want to write xml to isolated storage instead of storing just the values and creating the XML on the fly?Sensor
Did you ever get things figured out?Graven
G
16

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 };
Graven answered 23/9, 2011 at 22:1 Comment(5)
this is not possible since XmlWritterSettings.Ident is read-only and doing so throws an exception of type 'System.Xml.XmlException'Boaster
@r1verside looks public to me: msdn.microsoft.com/en-us/library/…. 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.Graven
The Settings property stays null for me. Used new XmlTextWriterEcdysis
@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.Brout
This creates an exception: System.Xml.XmlException: 'The 'XmlWriterSettings.Indent' property is read only and cannot be set.'Heuristic
F
28

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))
{
    ...
}
Flannel answered 23/9, 2011 at 21:59 Comment(0)
G
16

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 };
Graven answered 23/9, 2011 at 22:1 Comment(5)
this is not possible since XmlWritterSettings.Ident is read-only and doing so throws an exception of type 'System.Xml.XmlException'Boaster
@r1verside looks public to me: msdn.microsoft.com/en-us/library/…. 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.Graven
The Settings property stays null for me. Used new XmlTextWriterEcdysis
@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.Brout
This creates an exception: System.Xml.XmlException: 'The 'XmlWriterSettings.Indent' property is read only and cannot be set.'Heuristic
C
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.

Ciao answered 9/7, 2019 at 7:27 Comment(0)
D
-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.

Dehydrate answered 23/9, 2011 at 22:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.