问题
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 <p>.
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