Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / February 2008

Tip: Looking for answers? Try searching our database.

Save XmlDocument with hierarchical line breaks / indentation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fabian - 20 Feb 2008 10:10 GMT
I am creating a Xsl file programmatically with a XmlDocument object. I save
the document with XmlDocument(string Path).
All Nodes added to the document root are idented nicely. But any child nodes
to them are just written in the same line. Is there a possibility to modify
the saving? The hierarchy is correctly written - data-wise.

Example (incorrect version, see all the Children of "PropA" in a row instead
with newlines and tabs):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" />
 <xsl:template match="PropA">val1: <xsl:value-of select="val1" />val2:
<xsl:value-of select="val2" /></xsl:template>
</xsl:stylesheet>

correct version:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" />
 <xsl:template match="PropA">
    val1: <xsl:value-of select="val1"/>
    val2: <xsl:value-of select="val2"/>
 </xsl:template>
</xsl:stylesheet>

Thx in advance for your help,

Fabian
Martin Honnen - 20 Feb 2008 12:24 GMT
> Example (incorrect version, see all the Children of "PropA" in a row instead
> with newlines and tabs):
[quoted text clipped - 18 lines]
>   </xsl:template>
> </xsl:stylesheet>

The element has mixed contents (text children and element children) and
indenting that could change the semantics respectively it is not
necessarily clear how to indent that. You want the above format but
  <xsl:template match="PropA">
    val1:
    <xsl:value-of select="val1"/>
    ...
  </xsl:template>
is also possible.
So you will need to write your own XmlWriter that performs the
indentation you are looking for.
Or alternatively, as you are creating an XSLT stylesheet, you could wrap
the text in xsl:text e.g.
   <xsl:text>val1: </xsl:text>
then the xsl:template no longer has mixed contents and the child
elements would be indented as e.g.
  <xsl:template match="PropA">
    <xsl:text>val1: </xsl:text>
    <xsl:value-of select="val1"/>
    ...
  </xsl:template>

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Fabian - 20 Feb 2008 13:51 GMT
> Or alternatively, as you are creating an XSLT stylesheet, you could wrap
the text in xsl:text

This works but is not yet all I need: Can I influence the idendation?
Instead of

<xsl:template match="PropA">
<xsl:text>val1: </xsl:text>
<xsl:value-of select="val1"/>
</xsl:template>

I would like to have

<xsl:template match="PropA">
<xsl:text>val1: </xsl:text>  <xsl:value-of select="val1"/>
</xsl:template>

i.e. no line feed between the text node and the value. Is this somehow
possible? I know I can probably also make the output look like this with some
forward looking XPath query but this is probably not the easiest way, I
think.

Thx,

Fabian
Martin Honnen - 20 Feb 2008 14:05 GMT
> This works but is not yet all I need: Can I influence the idendation?
> Instead of
[quoted text clipped - 14 lines]
> forward looking XPath query but this is probably not the easiest way, I
> think.

As said, if you implement your own XmlWriter that does the indentation
you want then you can pass that to the Save method of an XmlDocument
instance. Or, as you are already using XmlDocument to create the XSLT
stylesheet, you can use PreserveWhitespace = true and then insert text
nodes as needed. I don't think there is anything in the .NET framework
that does what you want out of the box.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.