Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XML to TXT

2 views
Skip to first unread message

Tom Woods

unread,
Oct 14, 2009, 4:25:28 PM10/14/09
to

Is there a simple way to take a Xml.XmlDocument and save it as a TXT file
with formatting?

Thanks,
Tom


Martin Honnen

unread,
Oct 15, 2009, 5:45:50 AM10/15/09
to
Tom Woods wrote:
> Is there a simple way to take a Xml.XmlDocument and save it as a TXT file
> with formatting?

There is a Save method which has an overload that takes a file name:
http://msdn.microsoft.com/en-us/library/dw229a22.aspx
That serializes the DOM tree and saves it to a file.

If that is not what you want then please explain in more detail what
kind of "formatting" you have in mind.


--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

Tom Woods

unread,
Oct 15, 2009, 1:35:57 PM10/15/09
to
Sorry for being so vague. What I would like to do is to read the
XmlDocument line by line and add each line into a memo field.

When I use the InnerXml, it's just one long string with no CR/LF.

Can this be done?

Thanks,
Tom


"Martin Honnen" <maho...@yahoo.de> wrote in message
news:eHKbKxXT...@TK2MSFTNGP05.phx.gbl...

Martin Honnen

unread,
Oct 16, 2009, 5:29:53 AM10/16/09
to
Tom Woods wrote:
> Sorry for being so vague. What I would like to do is to read the
> XmlDocument line by line and add each line into a memo field.
>
> When I use the InnerXml, it's just one long string with no CR/LF.
>
> Can this be done?

The XML APIs don't expose XML line by line, rather they expose nodes.
But you can of course use the normal file IO APIs to read line by line.

And if InnerXml does not give you what you want then you might simply
want to set PreserveWhitespace to true before you load your XML document:

XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load("file.xml");
// now doc.OuterXml/InnerXml has any white space the original
document contains

Pete Delgado

unread,
Oct 28, 2009, 3:33:36 PM10/28/09
to

"Martin Honnen" <maho...@yahoo.de> wrote in message
news:emzQ7MkT...@TK2MSFTNGP06.phx.gbl...

Wouldn't it be far better and simpler to use XSLT to transform the XML into
the appropriate format?

-Pete


sloan

unread,
Nov 11, 2009, 9:10:33 AM11/11/09
to


I'm not sure if this is helpful.

But if you want to get the xml (mostly) the way it was........but have the
ability to update a few things......this is a (not yet proven) idea:
Well, it works as I have it below, but I don't know if it meets your need.


The below example will "copy" the xml to new xml, with one slight override.
The original WhoAmI (value) of DefaultWhoAmI will be replaced with a new
value of NOTDefaultWhoAmI.

Check the URL in the xsl comments for where I discovered this trick.

Save the xml as "myxml.xml" and the xsl as "myxsl.xsl".
The create a vbs file called "transform.vbs", and put the code I have below
in it.
Run transform.vbs and you should get a file called newStuff.xml.


------START XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>


<appSettings>
<add key ="WhoAmI" value="DefaultWhoAmI"/>
<add key ="ThisStays" value="TheSame"/>
</appSettings>


<someOtherElement>
<abc>123</abc>
<def>234</def>
<ghi>345</ghi>
</someOtherElement>

</configuration>
----------------------END XML


--------START XSL

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<!--http://adeneys.wordpress.com/2009/04/17/multi-environment-config/-->

<xsl:output method="xml" indent="yes"/>

<xsl:template
match="/configuration/appSettings/add[@key='WhoAmI']/@value">
<xsl:attribute name="value">NOTDefaultWhoAmI</xsl:attribute>
</xsl:template>

<!-- Default templates to match anything else -->
<xsl:template match="@*">
<xsl:copy/>
</xsl:template>

<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


--------END XSL

Start a vbs app to make the transformation
(call it transform.vbs or something)
-------------- START .VBS code

Option Explicit


Dim xmldoc 'As New MSXML2.DOMDocument40
set xmldoc = CREATEOBJECT("MSXML2.DOMDocument.4.0")
xmldoc.async = False
xmldoc.Load ".\myxml.xml"

Dim xsldoc 'As New MSXML2.DOMDocument40
set xsldoc = CREATEOBJECT("MSXML2.DOMDocument.4.0")
xsldoc.async = False
xsldoc.Load ".\myxsl.xsl"


dim result
result = xmldoc.transformNode(xsldoc)

'Dim XMLDoc
'Dim XSLDoc
'Set XMLDoc = WScript.CreateObject("MSXML.DOMDocument")
'XMLDoc.load WScript.Arguments(0)
'Set XSLDoc = WScript.CreateObject("MSXML.DOMDocument")
'XSLDoc.load WScript.Arguments(1)
Dim OutFile
Dim FSO
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
Set OutFile = FSO.CreateTextFile(".\newStuff.xml")
'OutFile.Write XMLDoc.transformNode(XSLDoc)
outfile.write result
OutFile.Close


"Tom Woods" <two...@gobaker.com> wrote in message
news:enyA5xQ...@TK2MSFTNGP02.phx.gbl...

sloan

unread,
Nov 11, 2009, 9:47:46 AM11/11/09
to
Agreed.

This seems to be calling for a tranform.

Here is my "to text file" hint.


<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />


The last line is the one of importance.

See my other post for a transform example.

"Pete Delgado" <Peter....@NoSpam.com> wrote in message
news:OMTIMWAW...@TK2MSFTNGP06.phx.gbl...

0 new messages