Innertext from XElement? [duplicate]

江枫思渺然 提交于 2019-12-22 05:13:37

问题


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;

I've also tried this:

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

I've also tried this:

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>..."

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.

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


回答1:


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

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

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



来源:https://stackoverflow.com/questions/21443421/innertext-from-xelement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!