Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Ascii到XML字符集的转换_C#_Xml_Visual C++ - Fatal编程技术网

C# Ascii到XML字符集的转换

C# Ascii到XML字符集的转换,c#,xml,visual-c++,C#,Xml,Visual C++,是否有任何类可以将ascii转换为xml字符集?最好是开源的。我将在vc++或C中使用这个类# 我的ascii有一些xml字符集中没有的可打印字符 我刚刚试着用ascii字符集发送简历,我试着把它存储在在线crm中,我收到了这个错误消息 javax.xml.bind.UnmarshalException -除此之外: [javax.xml.stream.XMLStreamException:ParseError位于[row,col]:[50,22] 消息:字符引用“”是无效的XML字符

是否有任何类可以将ascii转换为xml字符集?最好是开源的。我将在vc++或C中使用这个类#

我的ascii有一些xml字符集中没有的可打印字符

我刚刚试着用ascii字符集发送简历,我试着把它存储在在线crm中,我收到了这个错误消息

javax.xml.bind.UnmarshalException -除此之外: [javax.xml.stream.XMLStreamException:ParseError位于[row,col]:[50,22] 消息:字符引用“”是无效的XML字符。]


提前感谢

可能您还没有完全理解什么是字符集。XML不是字符集,尽管基于XML的输出确实使用字符集对数据进行编码


我建议你先阅读一下乔尔·斯波尔斯基的精彩文章,然后再回来看看你的问题。

字符参考

确实不是一篇好文章。您可能需要


您的文本不会有任何XML中不可用的可打印字符,但可能有一些XML中不可用的不可打印字符


特别是,Unicode值U+0000到U+001F是无效的,tab除外。回车和换行。如果您真的需要这些其他控制字符,您必须为它们创建自己的转义形式,并在另一端取消转义。

出于好奇,我花了几分钟时间用C编写了一个简单的例程,以输出128个ASCII字符的XML字符串,令我惊讶的是,.NET没有输出真正有效的XML文档。我想我输出元素文本的方式不太正确。无论如何,以下是代码(欢迎评论):

XmlDocument doc=新的XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration(“1.0”,“us ascii”),“”);
XmlElement elem=doc.CreateElement(“ASCII”);
附录子文件(elem);
字节[]b=新字节[1];
对于(int i=0;i<128;i++)
{
b[0]=转换为ToByte(i);
XmlElement e=doc.CreateElement(“ASCII_”+i.ToString().PadLeft(3,'0');
e、 InnerText=System.Text.AscienceODing.ASCII.GetString(b);
第二项要素(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>

�;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
!
"
#
$
%
&;
'
(
)
*
+
,
-
.
/
0
1.
2.
3.
4.
5.
6.
7.
8.
9
:
;
=
?
@
A.
B
C
D
E
F
G
H
我
J
K
L
M
N
O
P
Q
R
s
T
U
v
W
X
Y
Z
[
\
]
^
_
`
A.
B
C
D
E
F
G
H
我
J
K
L
M
N
o
P
Q
R
s
T
U
v
W
x
Y
Z
{
|
}
~
更新:

添加了带有“us ascii”编码的XML标记,您不需要额外的库来完成此操作。从不同的编码到嵌入的二进制数据,所有这些都可以通过通用的.net库实现。您能举个简单的例子吗?

我在使用C#中的OpenXML文档创建时遇到了与Excel相同的问题。
当生成带有错误ASCII字符的文档时,我的Excel导出功能将崩溃。
不知何故,我公司数据库中的字符串数据中有一些时髦的字符。
尽管我使用了他们OpenXMLSDK2.0中的MicrosoftDocumentFormat.OpenXML程序集,但在使用对象分配字符串值时,它仍然没有考虑到这一点

修复方法:

t.Text = Regex.Replace(sValue, @"[\x00-\x08]|[\x0B\x0C]|[\x0E-\x19]|[\uD800-\uDFFF]|[\uFFFE\uFFFF]", "?");
这将清除sValue字符串,方法是删除有问题的字符并将其替换为问号。您可以替换为任何字符串或仅使用空字符串

允许0x09(制表符)、0x0A(左前-换行或NL-新行)和0x0D(CR-回车)。上面的正则表达式注意不要删除这些

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

我曾想过在OpenXML中转义这些值,但当我查看输出时,它显示了excape标记。因此,Mike;TeeVee在Excel中仍然显示为Mike;TeeVee,而不是Mike;TeeVee或MikeLTeeVee。这就是我喜欢Mike?TeeVee方法的原因

我的直觉是,这是当前OpenXML中的一个错误,它编码允许的XML ASCII字符,但允许不支持的ASCII字符通过

更新:

t.Text = Regex.Replace(sValue, @"[\x00-\x08]|[\x0B\x0C]|[\x0E-\x19]|[\uD800-\uDFFF]|[\uFFFE\uFFFF]", "?");
我忘了我可以使用“OpenXMLSDK2.0生产力工具”查看这些字符的显示方式,以查看Excel之类的内部文档。
在那里我发现它使用的格式是:\ux0000\u

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

常规XML 1.1转义:

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])) + "_");
         });

您的Ascii集是否包含不可打印的字符?您的意思是“您能给出一个您期望的转换示例吗?所谓“xml字符集”,您是指用于在xml中引入某些字符的转义序列(例如等等)?如果您感兴趣,我的回答给出了如何处理此问题的代码。