简体   繁体   English

C# System.Xml.Linq.XElement.Value 移除嵌套标签

[英]C# System.Xml.Linq.XElement.Value removed nested tags

I have XElement like我有 XElement 喜欢

<text>The temperature is raised <b>75</b> to <b>80</b> Celsius</text>

I need to read the value of this XElement and element.value returns " The temperature is raised 75 to 80 Celcius "我需要读取此 XElement 的值,并且element.value返回“ The temperature is raised 75 to 80 Celcius

could someone please tell how can I get XElement value " The temperature is raised <b> 75 </b> to <b> 80 </b> Celsius "有人可以告诉我如何获得XElement值“温度升高<b> 75 </b><b> 80 </b>摄氏度

it seems iterating through elements using似乎遍历元素使用

var nodes = element.Nodes();
foreach (var node in nodes)
{

}

does not resolve the issue没有解决问题

I think you can simply use ToString() method to get the string representation of an XElement.我认为您可以简单地使用ToString()方法来获取 XElement 的字符串表示形式。

XElement element = new XElement("text", "The temperature is raised ", new XElement("b", "75"), " to ", new XElement("b", "80"), " Celsius");
string elementString = element.ToString();
Console.WriteLine(elementString);

Updated: Sorry, now only I understand your problem, try this.更新:对不起,现在只有我明白你的问题,试试这个。

string elementValue = "";
foreach (var node in element.Nodes())
{
    elementValue += node.ToString();
}
Console.WriteLine(elementValue);

Fiddle: https://do.netfiddle.net/ZsoG9F小提琴: https://do.netfiddle.net/ZsoG9F

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM