Groups | Blog | Home
all groups > dotnet xml > november 2006 >

dotnet xml : Xml and unicode values with &#x...


Tom Fields
11/25/2006 1:07:04 PM
Hello!

I like to use the XmlTextWriter to write some SVG files.

But in some cases, I need the '&' as '&' and not as &.

Example: <glyph unicode=3D"&#x4c;"/>

Some code-snippet:

XmlDocument ^ doc =3D gcnew XmlDocument();
....
XmlAttribute ^ a =3D doc->CreateAttribute("unicode");
a1->Value =3D ??? - what to write here?
...
Stream ^ fs =3D gcnew FileStream(filename, FileMode::Create);
...
XmlWriter^ writer =3D gcnew XmlTextWriter(fs, gcnew UTF8Encoding);
=


How can I force to write the attribute as &#x4c; and not as &amp;x4c;?

Thanks for any help in advance

Martin Honnen
11/25/2006 1:51:35 PM
[quoted text, click to view]

What programming language is that? You simply need to set the Value
property to a string with the Unicode characters you need, with C# or J#
you could simply use e.g.
"\u004C"
to have that character in a string literal.

Or you can simply use
"L"
as that is the character with the Unicode code point 76.

There is no need to escape such characters and there is no easy way to
use XmlDocument or XmlTextWriter to enforce escaping of such characters,
you would need your own custom XmlWriter that escapes the characters you
need.
The DOM does not preserve such numeric characters references, it always
gives you the character itself and not the reference.

--

Martin Honnen --- MVP XML
Tom Fields
11/26/2006 3:08:26 PM
Thanks for your answer.

Use managed c++.

For ansi-glyphs, I can use <glyph unicode=3D"A"/> and so on.
But for unicode glyphs I have to use <glyph unicode=3D"&#x1A12;"/>.

When I use

XmlAttribute ^ a1 =3D doc->CreateAttribute("unicode");
a1->Value =3D "\u01A5";

I get the following warning

warning C4566: character represented by universal-character-name =

'\u01A5' cannot be
represented in the currencode page (1252) =


und the result (in the xml-file is <glyph unicode=3D"?" />.

Martin Honnen
11/26/2006 3:52:05 PM
[quoted text, click to view]

Can you try
a1->Value = L"\u01A5";
?
I don't use managed C++ but some examples on managed C++ on MSDN use
that syntax which then converts to a managed .NET System.String literal
I hope. Generally I think the solution is to find a way to write managed
C++ and include/reference Unicode characters in the source code, not to
try to escape Unicode characters the XML way as XML can deal with
Unicode characters without escaping.



--

Martin Honnen --- MVP XML
Gadget
11/30/2006 12:00:00 AM
[quoted text, click to view]

You can use the InnerXml property instead, as this accepts an XML compliant
string such as "&#x01A5;".

Cheers,
AddThis Social Bookmark Button