By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,670 Members | 1,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,670 IT Pros & Developers. It's quick & easy.

LoadXml causing extra space in abbreviated end tag

P: n/a
Using .NET Framework 1.1 and Visual Studio .NET 2003. Language is VB.NET.

When loading an XML String using XMLDocument.LoadXml we are getting an extra
space inserted into abbreviated end tags:

objXmlDoc.XmlResolver = Nothing
objXmlDoc.LoadXml(XmlData)
....
....
objXmlDoc.InnerXml (gets written to database)

eg <blah/> becomes <blah /> (note extra space)

Is there any way to prevent this from happening??
Nov 12 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Matt Bush" <Ma******@discussions.microsoft.com> wrote in message news:F9**********************************@microsof t.com...
objXmlDoc.InnerXml (gets written to database)

eg <blah/> becomes <blah /> (note extra space)

Is there any way to prevent this from happening??


The simplest approach is instead of writing,

objXmlDoc.InnerXml

to the database, write,

objXml.Doc.InnerXml.Replace( " />", "/>")

Technically, this opens you up to inadvertantly replacing instances of " />"
in your text nodes if it ever appears, so while its the most expedient solution,
its also a practice to be discouraged.

Another way is to write to a StringWriter through an XmlTextWriter
passed to Save( ). This can be used to force empty elements to be
written as "<blah></blah>" pretty easily (it may do this by default,
people usually ask to go the other way around and that requires
overriding WriteFullEndElement( ) but in this case maybe you don't
have to override anything on the XmlTextWriter -- if you did it
would be to override WriteEndElement( ) to call WriteFullEnd-
Element( ) but like I said, it might not be strictly necessary.)

No, this isn't much comfort if you're going for the minimum number
of characters [begging the question why use XML if minimum data
rep size is the main design goal]; but as it showcases the last resort.
You can write your own context-sensitive StringWriter to strip that
space out if you really want.

As to why the representation with the extra space may be preferred,
according to the XML spec,

<blah/>
<blah />
<blah></blah>

are all the same [1]. However, one convention for HTML compatibility
is that <blah/> without the space should be avoided in favor for one with
a space, <blah /> [2].

One underlying reason for this suggestion was that lexical analyzers were
written incorrectly in some older HTML browsers to only look for a
trailing space (expecting ensuing attributes) or angle bracket, therefore
these older HTML browsers could misread the tag.
Derek Harmon
___
[1] - http://www.w3.org/TR/2004/REC-xml-20040204/#dt-empty
[2] - http://www.w3.org/TR/xhtml1/#C_2
Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.