locked
How to get xml string with spacing/indent

    Question

  • I'm using Windows.Data.Xml.Dom.XmlDocument to manipulate Xml in Javascript and get the xml String by GetXML(),

    however it seems no indent/spacing there, is there any way to get the indented xml string in javascript, or we have to do it by ourselves?

    Friday, May 4, 2012 3:18 AM

Answers

  • Yes, you can preserve whitespace but if there is none to begin with someone has to create it.  Why do you want whitespace?  That will be more characters that a parser would have to go over to process the XML.

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, May 10, 2012 7:04 PM
    Moderator
  • Most XML readers will or have an option to format the XML in a readable format. For example, open it in Internet Explorer and it will automatically apply an XSLT transform.  You of course can format the file yourself but it seems like unecessary work!

    -Jeff


    Jeff Sanders (MSFT)

    Friday, May 11, 2012 12:06 PM
    Moderator

All replies

  • Are you creating the XML using the XML objects or are you loading the string from some where?  Where and how are you displaying the XML? 

    If you are loading XML you can preserve the whitespace: http://msdn.microsoft.com/en-us/library/windows/apps/windows.data.xml.dom.xmlloadsettings.aspx

    If you need to display this but did not load it with whitespace (or are constructing the DOM on your own) you will need to format it yourself.

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, May 9, 2012 11:53 AM
    Moderator
  • I just write a SimpleJSON2XML utility func to convert a json object to xml, so i construct it by myself. Seems I have to do it manually...
    Thursday, May 10, 2012 7:39 AM
  • Yes, you can preserve whitespace but if there is none to begin with someone has to create it.  Why do you want whitespace?  That will be more characters that a parser would have to go over to process the XML.

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, May 10, 2012 7:04 PM
    Moderator
  • we will save the xml into external file. so we need indent for human-readability as data is huge.

    • Edited by Dong, Yi Friday, May 11, 2012 3:18 AM
    Friday, May 11, 2012 1:21 AM
  • Most XML readers will or have an option to format the XML in a readable format. For example, open it in Internet Explorer and it will automatically apply an XSLT transform.  You of course can format the file yourself but it seems like unecessary work!

    -Jeff


    Jeff Sanders (MSFT)

    Friday, May 11, 2012 12:06 PM
    Moderator