473,216 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 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 4810
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tom Petersen | last post by:
I am using a response.write to test the formatting of the output. I am supposed to get this: BEGIN:VCALENDAR VERSION:1.0 BEGIN:VEVENT DTSTART:20051022T090000Z DTEND:20051022T090000Z...
6
by: Tom Petersen | last post by:
Here is a little more info, sorry should have explained what my final goal was. I am creating a .vcs file from a form to import into Outlook. I was just testing the output on screen then pasting...
1
by: Riko Eksteen | last post by:
Hi I'm reading an xml file into an XmlDocument, adding some nodes, and writing it back out. I would like the nodes I add to assume the same level of indeting as the rest of the document. (I load...
2
by: Ken Wilson | last post by:
I am writing and .xml file and am not getting the formatting I would like. The portion of the code that is giving me problems is as follows; XmlTextWriter tw = new XmlTextWriter(filename); ...
2
by: shagy | last post by:
Hi, I'm having a problem with a <select><option> which has white space in values... When I post the data I only get the first word (up to the white space). "Testing white space" becomes...
25
by: mdh | last post by:
Hi Group, Not looking for an answer, but more of an explanation. Thinking back to those heady days when you had the time to do them, may I ask this. Exercise 1-22 asks for a program to "fold"...
0
by: NDK | last post by:
Ok so I have an html page with an embedded excel object. I load some information into the object from a spreadsheet. (works perfectly) I would like to be able to change the background of a few...
3
by: preethi303 | last post by:
Hi, I'm using VB 2005 and I have a problem formatting a string when I use the print preview dialog box. I'm using just the vbCrLf character and the Space function to correctly align my data and...
2
by: dmk | last post by:
Hi Again, I am reading a document into a page and then displaying it as source code (this is for an assignment.) I feel that the output that I am getting is a little hard to read, and want to...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.