c# - Xelement 属性值丢失其编码
问题描述
假设我们有这样的代码:
string actualvalue = "<a>";
string abc = "<firstnode><secondnode abc=\""+ actualvalue + "\"></secondnode></firstnode>";
XElement item2 = XElement.Parse(abc);
foreach (var item in item2.Descendants("secondnode"))
{
string aftergettingitfromXlement = item.Attribute("abc").Value;
///so aftergettingitfromXlement = "<a>" which is not correct i want to have actual value which is encoded
}
知道如何获得实际值,我不想再次编码。为什么编码丢失了?
解决方案
试试这个:
string actualvalue = "&lt;a&gt;";
XDocument
实现假定您的 xml 字符串中的值已转义。要阅读有关 xml 中的 escpae 字符的更多信息:我需要在 XML 文档中转义哪些字符?
要对字符串中的 & 符号进行一般替换,您可以编写:
string actualvalue = "<a>";
actualvalue = actualvalue.Replace("&", "&");
推荐阅读
- database - 在本地数据库中镜像部分 redux 状态
- postgresql - 如何在 PostgreSQL 中设置带有日期和时间的两列之间的间隔?
- javascript - 单击按钮时未发送 Django Post 请求?
- javascript - Django 在模板中错误地将 float x.xx 转换为 x,xx
- javascript - 如何禁用指针事件,但允许拖动
- c# - Web API 更新的令牌认证
- angular - Akita Collection Service 中的 SyncCollection 太贵了?
- javascript - 将 JSON 从 PHP 发送到 Javascript(未定义)
- scala - Scala 中协变 HashMap 的替代方案
- reactjs - 在 foreach 循环中从导入文件中获取函数