如何测试 XML 元素内部是否包含文本元素?

发布于 2024-09-25 18:37:50 字数 337 浏览 1 评论 0原文

我想知道如何测试以下情况:

<foo>
    <bla1>xxx</bla1>
    <bla2>yyy</bla2>
    <bla3>zzz</bla3>
</foo>

在 while(reader.Read()) 中,当我在 foo 和 bla1、bla2、bla3... 中时,我放入 XmlNodeType.Element xxx、yyy、zzz 中的 .TextElement。但是我可以测试 bla 内部是否有文本值吗?

非常感谢

佩德罗·杜索

I would like to know how could I test the fallowing situation:

<foo>
    <bla1>xxx</bla1>
    <bla2>yyy</bla2>
    <bla3>zzz</bla3>
</foo>

In the while(reader.Read()), I drop in the XmlNodeType.Element when I'm in the foo and bla1, bla2, bla3... When drop in the .TextElement in the xxx, yyy, zzz. But can I test if the bla's will have a text value inside or not?

Thanks very much

Pedro Dusso

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扭转时空 2024-10-02 18:37:50

不,只要您使用 XmlReader,就不能这样做

XmlReader 类通过XML 层次结构。因此,您只能对当前位置找到的 XML 节点进行操作。

在您的情况下,这意味着您将无法在不首先迭代“bla”节点的情况下检查它们的内容。

No, you can't as long as you are using an XmlReader.

The XmlReader class implements a forward-only cursor through an XML hieararchy. As such you can only operate on the XML node found at the current position.

In your case this means that you won't be able to check the contents of the "bla" nodes without first iterating through them.

帅气尐潴 2024-10-02 18:37:50

如果您使用的是 XmlTextReader,则可以使用属性 IsEmptyElement

If you are using an XmlTextReader you can use the property IsEmptyElement

倥絔 2024-10-02 18:37:50

reader.HasValue 有什么问题吗? (或 string.IsNullOrEmpty(reader.Value))?

Anything wrong with reader.HasValue? (or string.IsNullOrEmpty(reader.Value))?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文