471,122 Members | 859 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

Formatting White Space

Ot
I have an xml document similar to this

<all>
<one>
<options attr1="1" attr2="2" />
<item name="a name" attr1="1" />
<item name="another" attr1="2"/>
</one>
<two>
<options attr1="1" attr2="2" />
<item name="a name" attr1="1" />
<item name="another" attr1="2"/>
</all>

During processing I have occasion to delete an item. So, using the DOM
interface I delete the item.. Only thing is, the white space
(vbNewLine,vbTab,vbTab) that occurs before each item remains. When I insert
a new item the xml looks like this

<all>
<one>
<options attr1="1" attr2="2" />
<item name="a name" attr1="1" />

<item name="replacement item" attr1="2" />
</one>
<two>
<options attr1="1" attr2="2" />
<item name="a name" attr1="1" />
<item name="another" attr1="2"/>
</all>

The program works fine, but there is this accumulation of extra white-space.

As I insert an item I insert the appropriate TextNode.... for <one> and
<two> it has a single newline and tab character. Options, item inserts are
preceded by a newline and two tab characters for readability.

I would like to delete each item and the preceding textnode that contains
the alignment text. Any suggestions as to how to do this using DOM and
Xpath?

Nov 12 '05 #1
6 4690
Ot wrote:
I would like to delete each item and the preceding textnode that contains
the alignment text. Any suggestions as to how to do this using DOM and
Xpath?


Instead reformat XML during Save() operation (using
XmlTextWriter.Formatting property).
--
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com
Nov 12 '05 #2
Ot

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:O8**************@TK2MSFTNGP09.phx.gbl...
Ot wrote:
I would like to delete each item and the preceding textnode that contains the alignment text. Any suggestions as to how to do this using DOM and
Xpath?


Instead reformat XML during Save() operation (using
XmlTextWriter.Formatting property).


I currently have

Private doc As FreeThreadedDOMDocument40

I load it from an xml file with doc.load("c:\test.xml").

The xml, I presume, at this point is stored in memory.

I navigate around using Xpath on the "doc" making such changes as I want.
Presumably these are stored only in memory until I save the doc.

Each time I make a change to the data I doc.save("c:\test.xml"). Yes, this
saves it a lot of times, but the xml file is small, and I want to keep the
copy on disk current.

It is unclear to me how to use XmlTextWriter... that is, how do I tell the
save method to "indent".

Nov 12 '05 #3
To indent using the XmlTextWriter:

XmlTextWriter _writer = new XmlTextWriter(<parameters>);
_writer.Formatting = Formatting.Indented; (Indicates how the output is
formatted.)
_writer.Indentation = 1; (sets how many IndentChars to write for each level
in the hierarchy)
_writer.IndentChar = "\t"; (sets which character to use for indenting.
<tab> in this case)

....

_writer.Flush(); (used for saving)

Hope this helps,
Scott

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"Ot" <ur***@tds.invalid (use net)> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:O8**************@TK2MSFTNGP09.phx.gbl...
Ot wrote:
I would like to delete each item and the preceding textnode that contains the alignment text. Any suggestions as to how to do this using DOM and Xpath?
Instead reformat XML during Save() operation (using
XmlTextWriter.Formatting property).


I currently have

Private doc As FreeThreadedDOMDocument40

I load it from an xml file with doc.load("c:\test.xml").

The xml, I presume, at this point is stored in memory.

I navigate around using Xpath on the "doc" making such changes as I want.
Presumably these are stored only in memory until I save the doc.

Each time I make a change to the data I doc.save("c:\test.xml"). Yes,

this saves it a lot of times, but the xml file is small, and I want to keep the
copy on disk current.

It is unclear to me how to use XmlTextWriter... that is, how do I tell the
save method to "indent".

Nov 12 '05 #4
Ot
I know I'm missing something. I am not currently using XmlTextWriter nor
XmlTextReader. I am using the methods for DOMDocument40.

The .save method applied to the doc saves it where I want. I can see how
to create an XmlTextWriter. How do I get the doc.save method to use that
writer? doc.save("c:\test.xml") should be replaced with what? Do I
provide the XmlTextWriter to doc.save? Do I have to loop through my
in-memory xml domdocument40 and call on the XmlTextWriter for each node?
Sorry for the confusion.

The parameter to the save method is described as below.

oXMLDOMDocument.save(destination)Parameters
destination
An object. The object can represent a file name, an ASP Response object,
a DOMDocument object, or a custom object that supports persistence.
IXMLDocument Object Duplicates the original document. It is the
equivalent of saving the document and reparsing it. The document goes
through full persistence through XML markup, thereby testing the
persistability of your XML document.
<script language="jscript">
var xmldoc1 = new ActiveXObject("Msxml2.DOMDocument.4.0");
var xmldoc2 = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmldoc1.load("sample.xml");
xmldoc1.save(xmldoc2.XMLDocument);
</script>
Custom object supporting persistence Any other custom COM object that
supports QueryInterface for IStream, IPersistStream, or IPersistStreamInit
can also be provided here, and the document will be saved accordingly. In
the IStream case, the IStream Write method will be called as it saves the
document; in the IPersistStream case, IPersistStream Load will be called
with an IStream that supports the Read, Seek, and Stat methods.


"Scott Caskey [MSFT]" <sc*****@online.microsoft.com> wrote in message
news:40********@news.microsoft.com...
To indent using the XmlTextWriter:

XmlTextWriter _writer = new XmlTextWriter(<parameters>);
_writer.Formatting = Formatting.Indented; (Indicates how the output is
formatted.)
_writer.Indentation = 1; (sets how many IndentChars to write for each level in the hierarchy)
_writer.IndentChar = "\t"; (sets which character to use for indenting.
<tab> in this case)

...

_writer.Flush(); (used for saving)

Hope this helps,
Scott

--
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"Ot" <ur***@tds.invalid (use net)> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:O8**************@TK2MSFTNGP09.phx.gbl...
Ot wrote:

> I would like to delete each item and the preceding textnode that

contains
> the alignment text. Any suggestions as to how to do this using DOM and > Xpath?

Instead reformat XML during Save() operation (using
XmlTextWriter.Formatting property).


I currently have

Private doc As FreeThreadedDOMDocument40

I load it from an xml file with doc.load("c:\test.xml").

The xml, I presume, at this point is stored in memory.

I navigate around using Xpath on the "doc" making such changes as I want. Presumably these are stored only in memory until I save the doc.

Each time I make a change to the data I doc.save("c:\test.xml"). Yes,

this
saves it a lot of times, but the xml file is small, and I want to keep the copy on disk current.

It is unclear to me how to use XmlTextWriter... that is, how do I tell the save method to "indent".


Nov 12 '05 #5
Ot wrote:
I know I'm missing something. I am not currently using XmlTextWriter nor
XmlTextReader. I am using the methods for DOMDocument40.


Sorry Ot, but as you are posting to microsoft.public.dotnet.xml group we
both assumed you are using .NET XML classes.
In MSXML it's a bit different. I'm not sure if there is a way to indent
XML in MSXML other than using dummy XSLT transformation.
Alternatively you can try delete preceding white-space-only text nodes
along with each element to keep indenting.
--
Oleg Tkachenko
http://blog.tkachenko.com
Multiconn Technologies, Israel
Nov 12 '05 #6
Ot
Thanks for your patience with me.

I have switched from using the classes in MSXML and am using the ones in
System.Xml and all is well! Now declaring the file as a
System.Xml.XmlDocument and re-reading the replies here has allowed me to
get exactly where I want.

Thanks again all who helped!

Regards,
Ot

"Oleg Tkachenko" <oleg@no_!spam!_please!tkachenko.com> wrote in message
news:Ob**************@TK2MSFTNGP10.phx.gbl...
Ot wrote:
I know I'm missing something. I am not currently using XmlTextWriter nor XmlTextReader. I am using the methods for DOMDocument40.


Sorry Ot, but as you are posting to microsoft.public.dotnet.xml group we
both assumed you are using .NET XML classes.
In MSXML it's a bit different. I'm not sure if there is a way to indent
XML in MSXML other than using dummy XSLT transformation.
Alternatively you can try delete preceding white-space-only text nodes
along with each element to keep indenting.
--
Oleg Tkachenko
http://blog.tkachenko.com
Multiconn Technologies, Israel

Nov 12 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Tom Petersen | last post: by
6 posts views Thread by Tom Petersen | last post: by
1 post views Thread by Riko Eksteen | last post: by
2 posts views Thread by Ken Wilson | last post: by
2 posts views Thread by shagy | last post: by
25 posts views Thread by mdh | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.