为什么IXMLNode.IsTextElement对于CDATA元素不返回True? [英] Why doesn't IXMLNode.IsTextElement return True for CDATA elements?

查看:81
本文介绍了为什么IXMLNode.IsTextElement对于CDATA元素不返回True?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用的是Delphi 2007和 oxmldom Open XML 提供程序。

We're using Delphi 2007 and the oxmldom Open XML provider.

正常情况下的输入文件类似于:

A normal situations input file looks similar to this:

<root>
  <child>Some Text</child>
</root>

现在我们必须处理使用 CDATA 节点类型:

Now we have to process an input file that uses the CDATA node type:

<root>
  <child><![CDATA[Some special Text]]></child>
</root>

Node.IsTextElement 突然返回 False ,但 Node.Text 仍能按预期工作。

Node.IsTextElement suddenly returns False, but Node.Text still works as expected.

I知道 IXMLNode.IsTextElement 只是一种便捷方法,但我发现这种行为很奇怪。

I know that IXMLNode.IsTextElement is just a convenience method, but I find this behavior rather odd.

我们现在使用此自定义方法:

As a workaround we're now using this custom method:

class function TXmlUtils.IsTextOrCDataElement(ANode: IXMLNode): Boolean;
begin
  Result := False;
  if ANode.ChildNodes.Count = 0 then begin
    if ANode.NodeType in [ntText, ntCData] then begin
      Result := True;
    end;
  end else
  if ANode.ChildNodes.Count = 1 then begin
    if ANode.ChildNodes.First.NodeType in [ntText, ntCData] then begin
      Result := True;
    end;
  end;
end;

我的问题是:为什么 IsTextElement 不与 CDATA 节点一起使用,是否有更简单的解决方法?

My question is: Why does IsTextElement not work with CDATA nodes and is there an easier workaround?

推荐答案

它对于Cdata节点,它不会返回true,因为那不是它的编写方式。不幸的是,XML数据的使用者通常不必关心序列化数据中 文本的表示方式。您可以选择其他方法。

It doesn't return true for Cdata nodes because that's just not the way it was written. It's unfortunate because consumers of XML data generally shouldn't be concerned with how text is represented in the serialized data. Your alternative is fine.

这篇关于为什么IXMLNode.IsTextElement对于CDATA元素不返回True?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆