来自 XElement 的内文? [英] Innertext from XElement?

查看:24
本文介绍了来自 XElement 的内文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难从 XElement 的内部文本中获取正确的值.首先,这是我正在使用的 XML.这是我们工作流程中某个流程产生的生产数据的副本.换句话说,我无法更改 XML,我只能解析它.我想要获取其内部文本的元素内部具有看起来像 XML 的数据,但它不是.它是来自生成 XML 的工具的直接文本.该元素称为 :

I'm having a hard time getting the correct value from the innertext of an XElement. First, here's the XML that I'm using. This is a copy of our production data that results from a process in our workflow. In other words, I can't change the XML, I can only parse it. The element whose innertext I'd like to get has data inside that looks like XML, but it isn't. It is straight text from the tool that produced the XML. The element is called <creatorshapeutildata:

这是我尝试过的代码行:

Here is the line of code I've tried:

CreatorShapeUtilData = element.Descendants("creatorshapeutildata").Single().Value;

我也试过这个:

CreatorShapeUtilData = element.Descendants("creatorshapeutildata").First().Value;

我也试过这个:

CreatorShapeUtilData = element.Element("creatorshapeutildata").Value;

不幸的是,在每种情况下返回的值都是这样的:

Unfortunately, the value that gets returned in every case looks like this:

33012-1true#FFFF003#FFFFFF2743337743358

我需要返回的值看起来像这样:

I need the value returned to look like this:

"<creatorData type="object"><type type="int">33012</type>..."

我正在处理的这件作品是使用 XDocument、XElement 等的较大程序的一部分.我知道 XmlElement 具有 InnerText 属性,但我认为 XElement 没有,因为我似乎无法在智能感知.

This piece I'm working on is part of a larger program that uses XDocument, XElement, etc. I know an XmlElement has an InnerText property, but I think XElement does not, since I can't seem to find it in Intellisense.

那么,是否有任何可能的方法来获取 creatorshapeutil 标记之间的确切文本?

So, is there any possible way to grab the exact text between the creatorshapeutil tags?

推荐答案

您正试图获得与 InnerText/Value 属性完全相反的属性:原始 XML内容.

You're trying to get the exact opposite of the InnerText / Value properties: the raw XML content.

您可以通过调用element.ToString()获取包括外节点在内的内容.

You can get the content including the outer node by calling element.ToString().

如果要排除外层标签,可以调用String.Concat(element.Nodes()).

If you want to exclude the outer tag, you can call String.Concat(element.Nodes()).

这篇关于来自 XElement 的内文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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