473,473 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

XML Whitespace

Hello,
Is there an easy way to add whitespace to an XML document? I am getting a
stream from a WebDAV request and would like to display it in a readable
fashion in a text box.

Thanks,
Dan
Nov 12 '05 #1
7 5451
"solex" <so***@nowhere.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
Is there an easy way to add whitespace to an XML document?


Yes, write out the XML document into a String through an
XmlTextWriter with it's indenting turned on.

- - - XmlFormattedTextBox.cs (excerpt)
using System;
using System.IO;
using System.Text;
using System.Xml;
// . . .

// Create a string buffer and wrap it in a StringWriter (TextWriter)
// and XmlTextWriter (which needs a TextWriter to wrap).
//
StringBuilder stringBuf = new StringBuilder( );
StringWriter strWriter = new StringWriter( stringBuf);
XmlTextWriter xmlWriter = new XmlTextWriter( strWriter);

// Set formatting on the XmlTextWriter to indent.
//
xmlWriter.Formatting = Formatting.Indented;

// Write the XmlDocument containing the WebDAV request
// to the XmlTextWriter (it trickles down into the string buffer).
//
xmlDoc.WriteTo( xmlWriter);

// Set the TextBox's Text property to the String based on the
// string buffer that received the formatted XML serialization.
//
TextBox1.Text = stringBuf.ToString( );

// . . .
- - -
Derek Harmon
Nov 12 '05 #2
Derek,

Thanks for your suggestion, I used it to work out the below is the VB. With
regard to my code I would like to further optimize it by removing the xrsp
object (XmlDocument), from my limited understanding of streams it appears
that I do not need this object but should be able to pipe the StreamReader
directly in the the XmlTextWriter but cannot determine the appropriate
methods, and suggestions?

Thanks Again,
Dan
Dim wproc As New WebDav
Dim rsp As System.Net.HttpWebResponse

Try
Me.txtXMLResponse.Text = "Sending Request ...."
rsp = wproc.ProcessRequest(Me.txtURI.Text, _
CStr(Me.cmbMethod.SelectedItem), txtXMLRequest.Text)

Dim xrsp As New Xml.XmlDocument
Dim sr As New System.IO.StreamReader(rsp.GetResponseStream)
xrsp.LoadXml(sr.ReadToEnd)
sr.Close()
sr = Nothing

Dim buf As New System.Text.StringBuilder
Dim read As New System.IO.StringWriter(buf)
Dim xw As New System.Xml.XmlTextWriter(read)
xw.Formatting = Xml.Formatting.Indented
xw.Indentation = 3
xrsp.WriteTo(xw)
xw.Close()
xw = Nothing
Me.txtXMLResponse.Text = buf.ToString
Catch ex As Exception
Me.txtXMLResponse.Text = ex.ToString
Finally
wproc = Nothing
If Not IsNothing(rsp) Then
rsp.Close()
rsp = Nothing
End If
End Try

"Derek Harmon" <lo*******@msn.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"solex" <so***@nowhere.com> wrote in message

news:%2****************@TK2MSFTNGP11.phx.gbl...
Is there an easy way to add whitespace to an XML document?


Yes, write out the XML document into a String through an
XmlTextWriter with it's indenting turned on.

- - - XmlFormattedTextBox.cs (excerpt)
using System;
using System.IO;
using System.Text;
using System.Xml;
// . . .

// Create a string buffer and wrap it in a StringWriter (TextWriter)
// and XmlTextWriter (which needs a TextWriter to wrap).
//
StringBuilder stringBuf = new StringBuilder( );
StringWriter strWriter = new StringWriter( stringBuf);
XmlTextWriter xmlWriter = new XmlTextWriter( strWriter);

// Set formatting on the XmlTextWriter to indent.
//
xmlWriter.Formatting = Formatting.Indented;

// Write the XmlDocument containing the WebDAV request
// to the XmlTextWriter (it trickles down into the string buffer).
//
xmlDoc.WriteTo( xmlWriter);

// Set the TextBox's Text property to the String based on the
// string buffer that received the formatted XML serialization.
//
TextBox1.Text = stringBuf.ToString( );

// . . .
- - -
Derek Harmon

Nov 12 '05 #3
solex wrote:
Thanks for your suggestion, I used it to work out the below is the VB. With
regard to my code I would like to further optimize it by removing the xrsp
object (XmlDocument), from my limited understanding of streams it appears
that I do not need this object but should be able to pipe the StreamReader
directly in the the XmlTextWriter but cannot determine the appropriate
methods, and suggestions?


Not StreamReader, but XmlReader. Something like:

XmlReader r = new XmlTextReader(rsp.GetResponseStream());
StringWriter sw = new StringWriter();
XmlTextWriter w = new XmlTexWriter(sw);
w.Formatting = Formatting.Indented;
w.Indentation = 3;
while (r.Read())
w.WriteNode(r, false);
r.Close();
w.Close();
//Now use sw.ToString()

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4
Oleg,

Thanks for your reponse, but how do you handle a multi-part response (from
WEBDAV). I have tried as you suggested but get an error stating that "there
is no XML start tag open."

I have tried creating a new XMLTextWriter and StringReader within the loop
but still get the above error.

Thanks,
Dan
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:O1**************@TK2MSFTNGP09.phx.gbl...
solex wrote:
Thanks for your suggestion, I used it to work out the below is the VB. With regard to my code I would like to further optimize it by removing the xrsp object (XmlDocument), from my limited understanding of streams it appears that I do not need this object but should be able to pipe the StreamReader directly in the the XmlTextWriter but cannot determine the appropriate
methods, and suggestions?


Not StreamReader, but XmlReader. Something like:

XmlReader r = new XmlTextReader(rsp.GetResponseStream());
StringWriter sw = new StringWriter();
XmlTextWriter w = new XmlTexWriter(sw);
w.Formatting = Formatting.Indented;
w.Indentation = 3;
while (r.Read())
w.WriteNode(r, false);
r.Close();
w.Close();
//Now use sw.ToString()

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #5
solex wrote:
Thanks for your reponse, but how do you handle a multi-part response (from
WEBDAV). I have tried as you suggested but get an error stating that "there
is no XML start tag open."


XmlReader is a reader for XML and multi-part isn't XML.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #6
The response is a well-formed xml document, below is a sample. For some
reason the XMLReader is not reading the the root element and suggestions?

Thanks,
Dan

<?xml version="1.0"?>
<a:multistatus xmlns:b="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/"
xmlns:c="xml:" xmlns:a="DAV:">
<a:response>

<a:href>http://mailserver/Public/Private%20E...ommunications/
</a:href>
<a:propstat>
<a:status>HTTP/1.1 200 OK</a:status>
<a:prop>
<a:isfolder b:dt="boolean">1</a:isfolder>
</a:prop>
</a:propstat>
</a:response>
<a:response>

<a:href>http://mailserver/Public/Private%20E...20Revocable%20
Trust/</a:href>
<a:propstat>
<a:status>HTTP/1.1 200 OK</a:status>
<a:prop>
<a:isfolder b:dt="boolean">1</a:isfolder>
</a:prop>
</a:propstat>
</a:response>
<a:response>

<a:href>http://mailserver/Public/Private%20Equity/Deals/IDE%20and%20Co/</a:h
ref>
<a:propstat>
<a:status>HTTP/1.1 200 OK</a:status>
<a:prop>
<a:isfolder b:dt="boolean">1</a:isfolder>
</a:prop>
</a:propstat>
</a:response>
<a:response>

<a:href>http://mailserver/Public/Private%20Equity/Deals/ZXY%20Solutions/</a:
href>
<a:propstat>
<a:status>HTTP/1.1 200 OK</a:status>
<a:prop>
<a:isfolder b:dt="boolean">1</a:isfolder>
</a:prop>
</a:propstat>
</a:response>
<a:response>

<a:href>http://mailserver/Public/Private%20E...Dan,%20Revocab
le%20Trust/</a:href>
<a:propstat>
<a:status>HTTP/1.1 200 OK</a:status>
<a:prop>
<a:isfolder b:dt="boolean">1</a:isfolder>
</a:prop>
</a:propstat>
</a:response>
<a:response>

<a:href>http://mailserver/Public/Private%20E...st%20instituti
on/</a:href>
<a:propstat>
<a:status>HTTP/1.1 200 OK</a:status>
<a:prop>
<a:isfolder b:dt="boolean">1</a:isfolder>
</a:prop>
</a:propstat>
</a:response>
<a:response>

<a:href>http://mailserver/Public/Private%20Equity/Deals/1381289%20Limited/</
a:href>
<a:propstat>
<a:status>HTTP/1.1 200 OK</a:status>
<a:prop>
<a:isfolder b:dt="boolean">1</a:isfolder>
</a:prop>
</a:propstat>
</a:response>
<a:response>
<a:href>http://mailserver/Public/Private%20Equity/Deals/SPAGO/</a:href>
<a:propstat>
<a:status>HTTP/1.1 200 OK</a:status>
<a:prop>
<a:isfolder b:dt="boolean">1</a:isfolder>
</a:prop>
</a:propstat>
</a:response>
</a:multistatus>
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:Or*************@TK2MSFTNGP12.phx.gbl...
solex wrote:
Thanks for your reponse, but how do you handle a multi-part response (from WEBDAV). I have tried as you suggested but get an error stating that "there is no XML start tag open."


XmlReader is a reader for XML and multi-part isn't XML.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #7
solex wrote:
The response is a well-formed xml document, below is a sample. For some
reason the XMLReader is not reading the the root element and suggestions?


The document seems to be ok and I'm sure XmlReader can read it well.
Make sure there is no anything just before <?xml version="1.0"?> - it
must be the very first thing in XML document (except for optional
Unicode BOM characters).
Otherwise provide more info about the error.
--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Thomas Mlynarczyk | last post by:
Which is the simplest way to remove all whitespace from a string? Is there a simpler method than a regex replace? Or how can I tell a regex pattern to ignore all whitespace in my subject string?...
2
by: Wolfgang Jeltsch | last post by:
Hello, it is often convenient to insert whitespace into an XML document in order to format it nicely. For example, take this snippet of a notional DocBook XML document: <para> This is a...
2
by: Carlitos | last post by:
Hi there, A class in Xerces J-API (Java) called TextImpl contains a property that returns whether the text is ignorable whitespace...
2
by: Carlitos | last post by:
Hi there, A class in Xerces J-API (Java) called TextImpl contains a property that returns whether the text is ignorable whitespace...
0
by: Shan Plourde | last post by:
Hi everyone, I have been using various regular expressions with the ASP.NET RegularExpressionValidator for quite some time. In general it works very well. One of the common regex's that I use...
3
by: David Pratt | last post by:
Hi. I am splitting a string on a non whitespace character. One or more whitespace characters can be returned as items in the list. I do not want the items in the list that are only whitespace (can...
56
by: infidel | last post by:
Where are they-who-hate-us-for-our-whitespace? Are "they" really that stupid/petty? Are "they" really out there at all? "They" almost sound like a mythical caste of tasteless heathens that "we"...
9
by: amattie | last post by:
Does anyone have any idea on how I can strip the extra whitespace in the XML that shows up when I receive a response from an ASP.NET 2.0 webservice? This has been discussed before, but no one has...
5
by: John Gordon | last post by:
My XSLT files have many occurrences of this general pattern: <a> <xsl:attribute name="href"> <xsl:value-of select="xyz" /> </xsl:attribute> </a> When I execute an XSL transform, the...
13
by: Chaim Krause | last post by:
I am unable to figure out why the first two statements work as I expect them to and the next two do not. Namely, the first two spit the sentence into its component words, while the latter two...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.