XML与Delphi问题

XML与Delphi问题,xml,delphi,Xml,Delphi,我正在尝试实现一个协议,我将使用该协议使我的应用程序与服务器通信。问题是服务器正在使用XML,所以我尝试向服务器发送一个包含XML的字符串,但只得到了错误 当我发送此邮件时: mymsg: String = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+ '<m:outgoingEngineMessage xmlns:c="http://www.bvb.ro/xml/ns/arena/gw/constrain

我正在尝试实现一个协议,我将使用该协议使我的应用程序与服务器通信。问题是服务器正在使用XML,所以我尝试向服务器发送一个包含XML的字符串,但只得到了错误

当我发送此邮件时:

mymsg: String = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
'<m:outgoingEngineMessage xmlns:c="http://www.bvb.ro/xml/ns/arena/gw/constraints"'+
'xmlns:m="http://www.bvb.ro/xml/ns/arena/gw/msg"'+
'xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;'+
'&lt;content xsi:type=&quot;HeartBeatcmd&quot;&gt;'+
'&lt;/content&gt;'+
'&lt;csq&gt;100212&lt;/csq&gt;'+
'&lt;/m:outgoingEngineMessage&gt;';
mymsg:String=''+

生成“格式良好”的XML最安全的方法是使用XML库,如(开源)或MSXML库(Delphi为其提供了一个包装器)。

您正在逃避您不应该逃避的地方。仅当这些实体不是xml的一部分时才转义它们

像这样:

<content foo="bar">
2 + 2 &gt; 3
</content>
因此,您的xml将如下所示:

&lt;content foo=&quot;bar%quot;&gt;
2 + 2 &gt; 3
&lt;/content&gt;
mymsg: String = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
'<m:outgoingEngineMessage xmlns:c="http://www.bvb.ro/xml/ns/arena/gw/constraints" '+
'xmlns:m="http://www.bvb.ro/xml/ns/arena/gw/msg" '+
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
'<content xsi:type="HeartBeatcmd">'+
'</content>'+
'<csq>100212</csq>'+
'</m:outgoingEngineMessage>';
mymsg:String=''+
''+
''+
''+
'100212'+
'';

您还需要在每行末尾的属性之间的换行符处留出一个空格。你实际上是在把它们都挤在一起:

<m:outgoingEngineMessage xmlns:c="http://www.bvb.ro/xml/ns/arena/gw/constraints"'+
'xmlns:m="http://www.bvb.ro/xml/ns/arena/gw/msg"'+
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
'
将产生:

<m:outgoingEngineMessage xmlns:c="http://www.bvb.ro/xml/ns/arena/gw/constraints"xmlns:m="http://www.bvb.ro/xml/ns/arena/gw/msg"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

要解决此问题,您需要执行以下操作(基于@the_Fox的代码):

mymsg:String=''+
''+
''+
''+
'100212'+
'';

如果您在web上搜索带有“xmlns:xsi”的示例文档,很容易看到它们使用了类似于
xmlns:xsi=“…
的引号,而不是
xmlns:xsi=“…
”,以及其他明显的差异,如根元素的结束标记。在您的示例中,终止的
缺失。哎哟!不要手动生成“XML—使用库来实现这一点。从长远来看,这将为您节省很多麻烦。我仍然收到以下错误消息:元素类型“m:outgoingEngineMessage”后面必须跟属性规范“,”或“/”。XML和XHTML文档的属性之间必须有白色的WPACE,但代码会生成
../constraints”xmlns:m=“http…
,最好显示正确的XML代码(而不是Delphi常量声明),在关闭每行的字符串之前放置一个空格,例如
..gw/constants“+
是的,我们中有一些人:)
<m:outgoingEngineMessage xmlns:c="http://www.bvb.ro/xml/ns/arena/gw/constraints"xmlns:m="http://www.bvb.ro/xml/ns/arena/gw/msg"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
mymsg: String = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
'<m:outgoingEngineMessage xmlns:c="http://www.bvb.ro/xml/ns/arena/gw/constraints" '+
//                                                          see the space here --^
'xmlns:m="http://www.bvb.ro/xml/ns/arena/gw/msg" '+
//                                   and here --^
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
'<content xsi:type="HeartBeatcmd">'+
'</content>'+
'<csq>100212</csq>'+
'</m:outgoingEngineMessage>';