Writing formatted XML with XmlWriter

前端 未结 4 1579
臣服心动
臣服心动 2021-01-07 23:31

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


  

        
相关标签:
4条回答
  • 2021-01-07 23:54

    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.

    0 讨论(0)
  • 2021-01-07 23:59

    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 };
    
    0 讨论(0)
  • 2021-01-08 00:03

    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.

    0 讨论(0)
  • 2021-01-08 00:14

    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))
    {
        ...
    }
    
    0 讨论(0)
提交回复
热议问题