Innertext from XElement? [duplicate]

2019-04-28 10:41发布

This question already has an answer here:

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:

Picture of XML data

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?

标签: c# xml xelement
1条回答
老娘就宠你
2楼-- · 2019-04-28 10:52

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()).

查看更多
登录 后发表回答