Strings inside XML Tags

  • Thread starter Thread starter Roger Helliwell
  • Start date Start date
R

Roger Helliwell

I'm placing the contents of numerous TextBoxes into an XML file.
However, problems occur when the user enters any 'special' characters
in the text boxes. For example, punctuation marks such as < > & ' "

And vice versa... if there are &lt; &gt; &amp; tags within the XML, is
there an easy way to convert these back to unicode characters for
display? (There seems to be hundreds of &xxxx; codes listed in the
specs.)

Is there a general purpose method somewhere that does this kind of
conversion? Thanks.

Roger
 
Roger said:
I'm placing the contents of numerous TextBoxes into an XML file.
However, problems occur when the user enters any 'special' characters
in the text boxes. For example, punctuation marks such as < > & ' "

And vice versa... if there are &lt; &gt; &amp; tags within the XML, is
there an easy way to convert these back to unicode characters for
display? (There seems to be hundreds of &xxxx; codes listed in the
specs.)

Is there a general purpose method somewhere that does this kind of
conversion? Thanks.

Roger
a CDATA section should do it. You can store everything including the
terminator of the CDATA section.
 
I'm placing the contents of numerous TextBoxes into an XML file.
However, problems occur when the user enters any 'special' characters
in the text boxes. For example, punctuation marks such as < > & ' "

And vice versa... if there are < > & tags within the XML, is
there an easy way to convert these back to unicode characters for
display? (There seems to be hundreds of &xxxx; codes listed in the
specs.)

Is there a general purpose method somewhere that does this kind of
conversion? Thanks.

Roger

If you set the XmlNode.InnerText property to a string containing these
characters it will convert them (e.g. > to &gt;) and convert back when
you read it out. If you set it's InnerXml property then it won't do that
and you then get problems.
 
If you set the XmlNode.InnerText property to a string containing these
characters it will convert them (e.g. > to &gt;) and convert back when
you read it out. If you set it's InnerXml property then it won't do that
and you then get problems.

When I read an XML file (using node.InnerText), the special characters
get converted. When I'm saving the XML though, no conversions are
being performed. I'm using WriteElementString() as below:

XmlTextWriter tw = new XmlTextWriter("myalbums.xml", null);
tw.WriteStartElement("albumlist");

foreach (CAlbum album in collection) {
tw.WriteStartElement("album");
tw.WriteElementString("title", album.Title); // special
characters will get saved in the XML
tw.WriteElementString("artist", album.Artist);
tw.WriteElementString("year", album.Year.ToString() );
tw.WriteElementString("duration", album.Duration.ToString() );
tw.WriteEndElement();
}
tw.WriteEndElement();

Any further ideas? Thanks.
Roger
 
XmlTextWriter tw = new XmlTextWriter("myalbums.xml", null);
tw.WriteStartElement("albumlist");

foreach (CAlbum album in collection) {
tw.WriteStartElement("album");
tw.WriteElementString("title", album.Title); // special
characters will get saved in the XML
tw.WriteElementString("artist", album.Artist);
tw.WriteElementString("year", album.Year.ToString() );
tw.WriteElementString("duration", album.Duration.ToString() );
tw.WriteEndElement();
}
tw.WriteEndElement();

I see the problem now. WriteElementString() doesn't convert special
characters to character entities.
WriteString() does.

Roger
 
Back
Top