Encoding problems with XDocument XElement when using ReplaceWith method

大城市里の小女人 提交于 2020-01-11 13:22:10

问题


I have the following code:

XDocument doc = XDocument.Load(file);
var x = doc.Descendants("span");

XElement xelm = x.FirstOrDefault(xm => xm.Attribute("class").Value=="screenitems");

Regex rgx = new Regex("^<span class=\"screenitems\">(.*)</span>$");
Match mtc = rgx.Match(xelm.Value);
if (mtc.Success)
{
    xelm.ReplaceWith(mtc.Groups[1].Value);
}
doc.Save(file);

When I get a match and replace the content of the XML file loaded into variable doc using the ReplaceWith method for the XElement variable xelm, the content of the XML file is getting encoded, so instead of having a tag like <p> I get &lt;p&gt.

So How can I prevent that it encodes into html but actually replaces with the matched regex expression?

I have looked at some of the solutions here, like using XElement.Parse method or HTTPUtility.HtmlDecode, but I couldn't get it to work. It still encodes like html.


回答1:


While you could try and parse your RegEx match into XElements to solve the problem, I think you're going about this the wrong way.

As I understand it, your requirement is to replace a span element with the screenItems class with its contents. Rather than doing this using a combination of LINQ to XML and RegEx, you should stick to LINQ to XML.

Find all your span elements with the screenItems class:

var spans = doc.Descendants("span")
    .Where(e => (string)e.Attribute("class") == "screenItems")
    .ToList();

Then replace each of these with their own content:

foreach (var span in spans)
{
    span.ReplaceWith(span.Nodes());
}

See this fiddle for a working example.



来源:https://stackoverflow.com/questions/35001487/encoding-problems-with-xdocument-xelement-when-using-replacewith-method

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