473,382 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

XmlDocument converts "

Hello,
I' m using .NET System.Xml.XmlDOcument.
When I do the following:
XmlDocument xml = new XmlDocument();
xml.Load("blah");
....
xml.Save("blub");

I've got the problem that the following expression:
.... snip ...
<Comment>&apos;Depot&apos;</Comment>
.... snip ...

gets converted to

.... snip ...
<Comment>'Depot'</Comment>
.... snip ...

and the parser of another Company refuses to parse my Xml - File :-(
I've searched the web now quite a while, but didn't find any switch to say:
don't touch this special character.
amp stays, but quot and apos get converted to " and '

would be great if anybody could help me

thanks in advance
tobias
__********@gmx.net__
Nov 12 '05 #1
4 14737
barney wrote:
I've got the problem that the following expression:
... snip ...
<Comment>&apos;Depot&apos;</Comment>
... snip ...

gets converted to

... snip ...
<Comment>'Depot'</Comment>
... snip ...

and the parser of another Company refuses to parse my Xml - File :-(


They are using definitely severely broken parser. There is no difference
between &apos; and ' character - former is just a reference to it. They
are fully equivalent in XML.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2
"barney" <b-******@gmx.net> wrote in message news:dd**************************@posting.google.c om...
I've got the problem that the following expression:
... snip ...
<Comment>&apos;Depot&apos;</Comment>
... snip ...

gets converted to

... snip ...
<Comment>'Depot'</Comment>
... snip ...

and the parser of another Company refuses to parse my Xml - File :-(


Have you tried wrapping the output filename that you pass to the
XmlDocument::Save( ) method in an XmlTextWriter that substitutes
the &apos; and &quot; back into the text nodes that get written?

- - - KeepEntityXmlTextWriter.cs
using System;
using System.Diagnostics;
using System.IO;
using System.Xml;

public class KeepEntityXmlTextWriter : XmlTextWriter
{
private static readonly string[ ] ENTITY_SUBS = new string[] { "&apos;", "&quot;" };
private static readonly char[ ] REPLACE_CHARS = new char[] { '\'', '"' };
private bool insideAttribute;

public KeepEntityXmlTextWriter( string filename) : base( filename, null) { ; }

public override void WriteStartAttribute( string prefix, string localName, string ns)
{
this.insideAttribute = true;
base.WriteStartAttribute( prefix, localName, ns);
}

private void WriteStringWithReplace( string text)
{
string[ ] textSegments = text.Split( KeepEntityXmlTextWriter.REPLACE_CHARS);

if ( textSegments.Length > 1 )
{
for( int pos = -1, i = 0; i < textSegments.Length; ++i)
{
base.WriteString( textSegments[ i]);
pos += textSegments[ i].Length + 1;

// Assertion: Replace the following if-else when the number of
// replacement characters and substitute entities has grown
// greater than 2.
Debug.Assert( 2 == KeepEntityXmlTextWriter.REPLACE_CHARS.Length );

if ( pos != text.Length )
{
if ( text[ pos ] == KeepEntityXmlTextWriter.REPLACE_CHARS[ 0])
base.WriteRaw( KeepEntityXmlTextWriter.ENTITY_SUBS[ 0]);
else
base.WriteRaw( KeepEntityXmlTextWriter.ENTITY_SUBS[ 1]);
}
}
}
else base.WriteString( text);
}

public override void WriteString( string text)
{
if ( this.insideAttribute )
base.WriteString( text);
else
this.WriteStringWithReplace( text);
this.insideAttribute = false;
}
}
- - -

This specialized XmlTextWriter replaces ' and " in the XML written to a file
(hardcoded to UTF-8 encoding above, but you can add additional ctors)
when these characters appear in child text nodes. It doesn't touch these
characters when they appear in attribute values (depending on an attr's
quote character, one or the other character entity references may be
necessary for escapement). A WriteString( ) will occur after an Write-
StartAttribute( ) if the WriteString( ) is emitting an attribute value, so I
raise a flag in the overload of WriteStartAttribute( ) to detect this scenario.
Everytime WriteString( ) is called, it resets the flag.

When the presence of one or more of these replacement characters are
observed, the fragments of the text not containing these characters are
passed up to the base XmlTextWriter's WriteString( ) to emit them with
any necessary substitutions (like ampersand). To prevent XmlTextWriter
from converting the "&apos;" and "&quot;" entities into "&amp;apos;" and
"&amp;quot;" respectively, these must be written out using WriteRaw( )
to sidestep this conversion from happening.

If this other parser vendor's implementation of other characters gives you
further trouble, the KeepEntityXmlTextWriter should be straightforward to
extend for additional replacement characters and char entity ref substitutes.
Derek Harmon
Nov 12 '05 #3
Thanks Oleg,

but according to Xml rules I found on Internet you have to use the
following entities for those "special" characters:
< &lt;
&gt; & &amp;
" &quot;
' &apos;
I think I have to change them manually ...

bye tobias

"Oleg Tkachenko [MVP]" <oleg@no_!spam!_please!tkachenko.com> wrote in message news:<OH**************@TK2MSFTNGP12.phx.gbl>... barney wrote:
I've got the problem that the following expression:
... snip ...
<Comment>&apos;Depot&apos;</Comment>
... snip ...

gets converted to

... snip ...
<Comment>'Depot'</Comment>
... snip ...

and the parser of another Company refuses to parse my Xml - File :-(


They are using definitely severely broken parser. There is no difference
between &apos; and ' character - former is just a reference to it. They
are fully equivalent in XML.

Nov 12 '05 #4
barney wrote:
but according to Xml rules I found on Internet you have to use the
following entities for those "special" characters:


That's wrong. You MUST to escape < and & (XML API is doing it for you)
and MAY escape >, ' and " (usually it's done only when necessary).

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

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

Similar topics

2
by: DC Gringo | last post by:
I have an image control (that pulls an image off an ESRI map server): <ASP:IMAGE ID="imgZonedCountry" RUNAT="server"></ASP:IMAGE> In the code behind I am setting the ImageURL to a String value...
2
by: Eric Osman | last post by:
Hi, I'm looking for a javascript function that will convert input such as this: <CLUB Code=" into this: &lt;CLUB Code=&quot;
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
7
by: DC Gringo | last post by:
I am having a bear of a time with setting a URL query string as a text value in a dropdownlist and Server.URLEncode does not seem to do its job. theFullLink = theLinkPrefix &...
1
by: Lore Leunoeg | last post by:
Hello I want to load a local file into the XmlDocument cache. But when I'm not connected to the internet I get this exeption: "The underlying connection was closed: The remote name could not be...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
13
by: Ragnar | last post by:
Hi, 2 issues left with my tidy-work: 1) Tidy transforms a "&amp;" in the source-xml into a "&" in the tidied version. My XML-Importer cannot handle it 2) in a long <title>-string a wrap is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.