468,244 Members | 1,965 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,244 developers. It's quick & easy.

LoadXml causing extra space in abbreviated end tag

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

Similar topics

5 posts views Thread by Brian | last post: by
6 posts views Thread by jmgonet | last post: by
reply views Thread by Diane Yocom | last post: by
34 posts views Thread by Registered User | last post: by
2 posts views Thread by binder | last post: by
1 post views Thread by Adam Sandler | last post: by
8 posts views Thread by dancer | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.