【问题标题】:Xelement attribute value loses its encodingXelement 属性值丢失其编码
【发布时间】:2019-10-09 12:33:21
【问题描述】:

假设我们有这样的代码:

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 
}

知道如何获得实际值,我不想再次编码。为什么编码丢失了?

【问题讨论】:

    标签: c# encoding xelement


    【解决方案1】:

    试试这个:

    string actualvalue = "&amp;lt;a&amp;gt;";
    

    XDocument 实现假定您的 xml 字符串中的值已转义。 阅读有关 xml 中 escpae 字符的更多信息:What characters do I need to escape in XML documents?

    要对字符串中的 & 符号进行一般替换,您可以编写:

    string actualvalue = "&lt;a&gt;";
    actualvalue = actualvalue.Replace("&", "&amp;");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 1970-01-01
      • 2011-06-03
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多