Connecting Tech Pros Worldwide Forums | Help | Site Map

Formatting White Space

Ot
Guest
 
Posts: n/a
#1: Nov 12 '05
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?






Oleg Tkachenko
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Formatting White Space


Ot wrote:
[color=blue]
> 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?[/color]

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

re: Formatting White Space



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

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".



Scott Caskey [MSFT]
Guest
 
Posts: n/a
#4: Nov 12 '05

re: Formatting White Space


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" <uraza@tds.invalid (use net)> wrote in message
news:%23gmZHJS4DHA.876@TK2MSFTNGP10.phx.gbl...[color=blue]
>
> "Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
> news:O8SthSR4DHA.2572@TK2MSFTNGP09.phx.gbl...[color=green]
> > Ot wrote:
> >[color=darkred]
> > > I would like to delete each item and the preceding textnode that[/color][/color]
> contains[color=green][color=darkred]
> > > the alignment text. Any suggestions as to how to do this using DOM[/color][/color][/color]
and[color=blue][color=green][color=darkred]
> > > Xpath?[/color]
> >
> > Instead reformat XML during Save() operation (using
> > XmlTextWriter.Formatting property).[/color]
>
> 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,[/color]
this[color=blue]
> 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".
>
>
>[/color]


Ot
Guest
 
Posts: n/a
#5: Nov 12 '05

re: Formatting White Space


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]" <scaskey@online.microsoft.com> wrote in message
news:40116800$1@news.microsoft.com...[color=blue]
> 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[/color]
level[color=blue]
> 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[/color]
rights.[color=blue]
> Use of included script samples are subject to the terms specified at
> http://www.microsoft.com/info/cpyright.htm
>
> "Ot" <uraza@tds.invalid (use net)> wrote in message
> news:%23gmZHJS4DHA.876@TK2MSFTNGP10.phx.gbl...[color=green]
> >
> > "Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
> > news:O8SthSR4DHA.2572@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > Ot wrote:
> > >
> > > > I would like to delete each item and the preceding textnode that[/color]
> > contains[color=darkred]
> > > > the alignment text. Any suggestions as to how to do this using DOM[/color][/color]
> and[color=green][color=darkred]
> > > > Xpath?
> > >
> > > Instead reformat XML during Save() operation (using
> > > XmlTextWriter.Formatting property).[/color]
> >
> > 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[/color][/color]
want.[color=blue][color=green]
> > 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,[/color]
> this[color=green]
> > saves it a lot of times, but the xml file is small, and I want to keep[/color][/color]
the[color=blue][color=green]
> > copy on disk current.
> >
> > It is unclear to me how to use XmlTextWriter... that is, how do I tell[/color][/color]
the[color=blue][color=green]
> > save method to "indent".
> >
> >
> >[/color]
>
>[/color]


Oleg Tkachenko
Guest
 
Posts: n/a
#6: Nov 12 '05

re: Formatting White Space


Ot wrote:
[color=blue]
> I know I'm missing something. I am not currently using XmlTextWriter nor
> XmlTextReader. I am using the methods for DOMDocument40.[/color]

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
Ot
Guest
 
Posts: n/a
#7: Nov 12 '05

re: Formatting White Space


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:Ob3SHIf4DHA.2132@TK2MSFTNGP10.phx.gbl...[color=blue]
> Ot wrote:
>[color=green]
> > I know I'm missing something. I am not currently using XmlTextWriter[/color][/color]
nor[color=blue][color=green]
> > XmlTextReader. I am using the methods for DOMDocument40.[/color]
>
> 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[/color]


Closed Thread