P
preport
I'm trying to ensure that all the characters in my XML document are
characters specified in this document:
http://www.w3.org/TR/2000/REC-xml-20001006#charsets
Would a function like this work:
private static string formatXMLString(string n)
{
if (string.IsNullOrEmpty(n)) return n;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
char[] chrs = n.ToCharArray();
char c;
int x, j = chrs.Length;
for (x = 0; x < j; x++)
{
c = chrs[x];
if (c == 0x9 || c == 0xA || c == 0xD ||
(c > 0x20 && c < 0xd7ff) ||
(c > 0xe000 && c < 0xffd) ||
(c > 0x10000 && c < 0x10ffff))
{
sb.Append(c);
}
}
return sb.ToString();
}
I've never compared characters to like this (0x9, 0xffd, etc...)?
I'm not trying to be lazy and not test it myself, I just don't know if this
type of character comparison is the correct logic for the results I'm
looking for.
Any input?
characters specified in this document:
http://www.w3.org/TR/2000/REC-xml-20001006#charsets
Would a function like this work:
private static string formatXMLString(string n)
{
if (string.IsNullOrEmpty(n)) return n;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
char[] chrs = n.ToCharArray();
char c;
int x, j = chrs.Length;
for (x = 0; x < j; x++)
{
c = chrs[x];
if (c == 0x9 || c == 0xA || c == 0xD ||
(c > 0x20 && c < 0xd7ff) ||
(c > 0xe000 && c < 0xffd) ||
(c > 0x10000 && c < 0x10ffff))
{
sb.Append(c);
}
}
return sb.ToString();
}
I've never compared characters to like this (0x9, 0xffd, etc...)?
I'm not trying to be lazy and not test it myself, I just don't know if this
type of character comparison is the correct logic for the results I'm
looking for.
Any input?