Ascii到XML字符集转换

时间:2009-11-14 06:55:37

标签: c# xml visual-c++

是否有任何类将ascii转换为xml characterset最好是开源我将在vc ++或C#中使用这个类

我的ascii有一些可打印的字符,这些字符在xml字符集中不存在

我只是试图发送一个ascii字符集的简历,我试图将它存储在一个在线crm中,我收到了此错误消息

javax.xml.bind.UnmarshalException   - 链接异常: [javax.xml.stream.XMLStreamException:[row,col]处的ParseError:[50,22] 消息:字符引用“&#x13”是无效的XML字符。]

提前致谢

6 个答案:

答案 0 :(得分:8)

我在使用C#中的OpenXML文档创建时遇到了同样的问题 构建具有错误ASCII字符的doc时,我的Excel导出功能会爆炸 在我公司的数据库中,字符串数据在某种程度上具有时髦的字符 尽管我在OpenXML SDK 2.0中使用了Microsoft DocumentFormat.OpenXML程序集,但在使用对象分配字符串值时仍然没有注意这一点。

修复:

t.Text = Regex.Replace(sValue, @"[\x00-\x08]|[\x0B\x0C]|[\x0E-\x19]|[\uD800-\uDFFF]|[\uFFFE\uFFFF]", "?");

通过删除有问题的字符并用问号替换它来清除sValue字符串。你可以用任何字符串替换或只使用空字符串。

XML Spec允许0x09(TAB),0x0A(LF - 换行或NL - 换行)和0x0D(CR - 回车)。上面的RegEx注意不要删除它们。

XML 1.1规范允许您转义其中一些字符 例如:使用 for 0x03显示为?在HTML和Office文档和记事本中的 L 我使用Asp.net,这在我的GridView中自动处理,所以我不需要替换这些值 - 但我相信它可能是浏览器为我所知道的所有人处理它。

我想过在OpenXML中转义这些值,但是当我查看输出时,它显示了excape标记。所以Mike TeeVee仍然在Excel中显示为Mike TeeVee而不是Mike?TeeVee或者Mike L TeeVee。这就是为什么我更喜欢Mike?TeeVee的方法。

我的预感是这是当前OpenXML中的一个错误,它编码允许的XML ASCII字符,但允许不支持的ASCII字符滑过。

<强>更新

我忘了我可以使用“ Open XML SDK 2.0 Productivity Tool ”来查看这些字符的显示方式,以查看Excel等内部文档。
在那里,我发现它使用的格式为: _ x0000 _

请记住:XML 1.0不支持转义这些值,但XML 1.1支持,因此如果您使用的是1.1,那么您可以使用此代码来转义它们。

Regular XML 1.1 Escaping:

t.Text = Regex.Replace(s, @"[\x00-\x08]|[\x0B\x0C]|[\x0E-\x19]|[\uD800-\uDFFF]|[\uFFFE\uFFFF]",
         delegate(Match m)
         {
           return (byte)(m.Value[0]) == 0 //0x00 is not Supported in 1.0 or 1.1
                  ? ""
                  : ("&#x" + string.Format("{0:00}", (byte)(m.Value[0])) + ";");
         });


如果您要转义OpenXML的字符串,请改用它:

t.Text = Regex.Replace(s, @"[\x00-\x08]|[\x0B\x0C]|[\x0E-\x19]|[\uD800-\uDFFF]|[\uFFFE\uFFFF]",
         delegate(Match m)
         {
           return (byte)(m.Value[0]) == 0 //0x00 is not Supported in 1.0 or 1.1
                  ? ""
                  : ("_x" + string.Format("{0:0000}", (byte)(m.Value[0])) + "_");
         });

答案 1 :(得分:7)

您的文字中没有任何可打印的字符,这些字符在XML中不可用 - 但它可能包含一些不可打印的字符,这些字符在XML中不可用。< / p>

特别是,Unicode值U + 0000到U + 001F无效除了标签。回车和换行。如果你真的需要那些其他控制角色,你必须为它们创建自己的转义形式,并在另一端转移它们。

答案 2 :(得分:3)

字符引用&#x13确实不是valid XML character。您可能需要&#xD&#13

答案 3 :(得分:2)

出于好奇,我花了几分钟在C#中写了一个简单的例程来抽出128个ASCII字符的XML字符串,令我惊讶的是,.NET没有输出真正有效的XML文档。我想我输出元素文本的方式并不完全正确。无论如何这里是代码(欢迎评论):

XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "us-ascii", ""));
XmlElement elem = doc.CreateElement("ASCII");
doc.AppendChild(elem);
byte[] b = new byte[1];
for (int i = 0; i < 128; i++)
{
    b[0] = Convert.ToByte(i);
    XmlElement e = doc.CreateElement("ASCII_" + i.ToString().PadLeft(3,'0'));
    e.InnerText = System.Text.ASCIIEncoding.ASCII.GetString(b);
    elem.AppendChild(e);
}
Console.WriteLine(doc.OuterXml);

这是格式化的输出:

<?xml version="1.0" encoding="us-ascii" ?>
    <ASCII>
    <ASCII_000>&#x0;</ASCII_000>
    <ASCII_001>&#x1;</ASCII_001>
    <ASCII_002>&#x2;</ASCII_002>
    <ASCII_003>&#x3;</ASCII_003>
    <ASCII_004>&#x4;</ASCII_004>
    <ASCII_005>&#x5;</ASCII_005>
    <ASCII_006>&#x6;</ASCII_006>
    <ASCII_007>&#x7;</ASCII_007>
    <ASCII_008>&#x8;</ASCII_008>
    <ASCII_009> </ASCII_009>
    <ASCII_010>
    </ASCII_010>
    <ASCII_011>&#xB;</ASCII_011>
    <ASCII_012>&#xC;</ASCII_012>
    <ASCII_013>
    </ASCII_013>
    <ASCII_014>&#xE;</ASCII_014>
    <ASCII_015>&#xF;</ASCII_015>
    <ASCII_016>&#x10;</ASCII_016>
    <ASCII_017>&#x11;</ASCII_017>
    <ASCII_018>&#x12;</ASCII_018>
    <ASCII_019>&#x13;</ASCII_019>
    <ASCII_020>&#x14;</ASCII_020>
    <ASCII_021>&#x15;</ASCII_021>
    <ASCII_022>&#x16;</ASCII_022>
    <ASCII_023>&#x17;</ASCII_023>
    <ASCII_024>&#x18;</ASCII_024>
    <ASCII_025>&#x19;</ASCII_025>
    <ASCII_026>&#x1A;</ASCII_026>
    <ASCII_027>&#x1B;</ASCII_027>
    <ASCII_028>&#x1C;</ASCII_028>
    <ASCII_029>&#x1D;</ASCII_029>
    <ASCII_030>&#x1E;</ASCII_030>
    <ASCII_031>&#x1F;</ASCII_031>
    <ASCII_032> </ASCII_032>
    <ASCII_033>!</ASCII_033>
    <ASCII_034>"</ASCII_034>
    <ASCII_035>#</ASCII_035>
    <ASCII_036>$</ASCII_036>
    <ASCII_037>%</ASCII_037>
    <ASCII_038>&amp;</ASCII_038>
    <ASCII_039>'</ASCII_039>
    <ASCII_040>(</ASCII_040>
    <ASCII_041>)</ASCII_041>
    <ASCII_042>*</ASCII_042>
    <ASCII_043>+</ASCII_043>
    <ASCII_044>,</ASCII_044>
    <ASCII_045>-</ASCII_045>
    <ASCII_046>.</ASCII_046>
    <ASCII_047>/</ASCII_047>
    <ASCII_048>0</ASCII_048>
    <ASCII_049>1</ASCII_049>
    <ASCII_050>2</ASCII_050>
    <ASCII_051>3</ASCII_051>
    <ASCII_052>4</ASCII_052>
    <ASCII_053>5</ASCII_053>
    <ASCII_054>6</ASCII_054>
    <ASCII_055>7</ASCII_055>
    <ASCII_056>8</ASCII_056>
    <ASCII_057>9</ASCII_057>
    <ASCII_058>:</ASCII_058>
    <ASCII_059>;</ASCII_059>
    <ASCII_060>&lt;</ASCII_060>
    <ASCII_061>=</ASCII_061>
    <ASCII_062>&gt;</ASCII_062>
    <ASCII_063>?</ASCII_063>
    <ASCII_064>@</ASCII_064>
    <ASCII_065>A</ASCII_065>
    <ASCII_066>B</ASCII_066>
    <ASCII_067>C</ASCII_067>
    <ASCII_068>D</ASCII_068>
    <ASCII_069>E</ASCII_069>
    <ASCII_070>F</ASCII_070>
    <ASCII_071>G</ASCII_071>
    <ASCII_072>H</ASCII_072>
    <ASCII_073>I</ASCII_073>
    <ASCII_074>J</ASCII_074>
    <ASCII_075>K</ASCII_075>
    <ASCII_076>L</ASCII_076>
    <ASCII_077>M</ASCII_077>
    <ASCII_078>N</ASCII_078>
    <ASCII_079>O</ASCII_079>
    <ASCII_080>P</ASCII_080>
    <ASCII_081>Q</ASCII_081>
    <ASCII_082>R</ASCII_082>
    <ASCII_083>S</ASCII_083>
    <ASCII_084>T</ASCII_084>
    <ASCII_085>U</ASCII_085>
    <ASCII_086>V</ASCII_086>
    <ASCII_087>W</ASCII_087>
    <ASCII_088>X</ASCII_088>
    <ASCII_089>Y</ASCII_089>
    <ASCII_090>Z</ASCII_090>
    <ASCII_091>[</ASCII_091>
    <ASCII_092>\</ASCII_092>
    <ASCII_093>]</ASCII_093>
    <ASCII_094>^</ASCII_094>
    <ASCII_095>_</ASCII_095>
    <ASCII_096>`</ASCII_096>
    <ASCII_097>a</ASCII_097>
    <ASCII_098>b</ASCII_098>
    <ASCII_099>c</ASCII_099>
    <ASCII_100>d</ASCII_100>
    <ASCII_101>e</ASCII_101>
    <ASCII_102>f</ASCII_102>
    <ASCII_103>g</ASCII_103>
    <ASCII_104>h</ASCII_104>
    <ASCII_105>i</ASCII_105>
    <ASCII_106>j</ASCII_106>
    <ASCII_107>k</ASCII_107>
    <ASCII_108>l</ASCII_108>
    <ASCII_109>m</ASCII_109>
    <ASCII_110>n</ASCII_110>
    <ASCII_111>o</ASCII_111>
    <ASCII_112>p</ASCII_112>
    <ASCII_113>q</ASCII_113>
    <ASCII_114>r</ASCII_114>
    <ASCII_115>s</ASCII_115>
    <ASCII_116>t</ASCII_116>
    <ASCII_117>u</ASCII_117>
    <ASCII_118>v</ASCII_118>
    <ASCII_119>w</ASCII_119>
    <ASCII_120>x</ASCII_120>
    <ASCII_121>y</ASCII_121>
    <ASCII_122>z</ASCII_122>
    <ASCII_123>{</ASCII_123>
    <ASCII_124>|</ASCII_124>
    <ASCII_125>}</ASCII_125>
    <ASCII_126>~</ASCII_126>
    <ASCII_127></ASCII_127>
</ASCII>

<强>更新
使用“us-ascii”编码

添加了XML decalration

答案 4 :(得分:1)

可能你不完全理解字符集是什么。 XML不是一个字符集,尽管基于XML的输出确实使用字符集来编码数据。

我建议您阅读Joel Spolsky的优秀帖子The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!),然后再回来再问你的问题。

答案 5 :(得分:0)

您不需要额外的库来执行此操作。从不同的编码到嵌入式二进制数据,所有这些都可以通过通用的.net库实现。你能举一个简单的例子吗?