473,796 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,vbTa b,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="replaceme nt 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 4841
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.F ormatting property).
--
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com
Nov 12 '05 #2
Ot

"Oleg Tkachenko" <oleg@NO!SPAM!P LEASEtkachenko. com> wrote in message
news:O8******** ******@TK2MSFTN GP09.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.F ormatting property).


I currently have

Private doc As FreeThreadedDOM Document40

I load it from an xml file with doc.load("c:\te st.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:\te st.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.Formatt ing = Formatting.Inde nted; (Indicates how the output is
formatted.)
_writer.Indenta tion = 1; (sets how many IndentChars to write for each level
in the hierarchy)
_writer.IndentC har = "\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.inva lid (use net)> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .

"Oleg Tkachenko" <oleg@NO!SPAM!P LEASEtkachenko. com> wrote in message
news:O8******** ******@TK2MSFTN GP09.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.F ormatting property).


I currently have

Private doc As FreeThreadedDOM Document40

I load it from an xml file with doc.load("c:\te st.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:\te st.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:\te st.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(destinati on)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="jscri pt">
var xmldoc1 = new ActiveXObject(" Msxml2.DOMDocum ent.4.0");
var xmldoc2 = new ActiveXObject(" Msxml2.DOMDocum ent.4.0");
xmldoc1.load("s ample.xml");
xmldoc1.save(xm ldoc2.XMLDocume nt);
</script>
Custom object supporting persistence Any other custom COM object that
supports QueryInterface for IStream, IPersistStream, or IPersistStreamI nit
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.Formatt ing = Formatting.Inde nted; (Indicates how the output is
formatted.)
_writer.Indenta tion = 1; (sets how many IndentChars to write for each level in the hierarchy)
_writer.IndentC har = "\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.inva lid (use net)> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .

"Oleg Tkachenko" <oleg@NO!SPAM!P LEASEtkachenko. com> wrote in message
news:O8******** ******@TK2MSFTN GP09.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.F ormatting property).


I currently have

Private doc As FreeThreadedDOM Document40

I load it from an xml file with doc.load("c:\te st.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:\te st.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.publi c.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.XmlD ocument 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!tkachen ko.com> wrote in message
news:Ob******** ******@TK2MSFTN GP10.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.publi c.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
3758
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 LOCATION;ENCODING=QUOTED-PRINTABLE:Library UID:20051022T090000ZI-request DESCRIPTION;ENCODING=QUOTED-PRINTABLE:test
6
1741
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 that into a file, after removing the extra white space, and inserting line breaks. The data is valid, but the formatting into the file isn't working. Was I doing the formatting right if I was generating a file? I need it to create the .vcs...
1
10225
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 the document with PreserveWhiteSpace = true.) I thought I could do this by using an XmlTextWriter with Formatting set to Formatting.Indented, but this doesn't work. The Formatting.Indented on the XmlTextWriter only indents my output when I...
2
6328
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); tw.Formatting = Formatting.Indented; tw.WriteStartDocument(); tw.WriteStartElement("MyRoot"); tw.WriteStartElement("MyString);
2
6667
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 "Testing" after posting code... <select name="descr" id="descr">
25
2711
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" long input lines into 2 or more shorter lines before the nth column... etc etc. Now, there are numerous anwers on the web and in the "C answer book", which includes a function to expand the tabs to blanks ( depending upon where the tab is in...
0
3039
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 cells in the object based on there new values. How do I do this? I've done this before with VBA scripts in excel using the cell.Interior.ColorIndex property, but it definitely doesn't work with this object. Any help would be much appreciated. I've...
3
1513
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 when I write this data to a textfile, it's formatted the way I want it to be. But when I display it with the print preview dialog, the format is wrong. I was able to trace the problem to either the Space function or the font. When writing to a file,...
2
1226
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 go beyond the scope of the assignment. I want to add formatting (just like what the tags on this forum does. Does anyone know of a simple to implement API for ASP.NET that will allow me to implement this using the method of input below? ...
0
9527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.