Skip to content Skip to sidebar Skip to footer

Encoding Problems With Xdocument Xelement When Using Replacewith Method

I have the following code: XDocument doc = XDocument.Load(file); var x = doc.Descendants('span'); XElement xelm = x.FirstOrDefault(xm => xm.Attribute('class').Value=='screenite

Solution 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 (varspan in spans)
{
    span.ReplaceWith(span.Nodes());
}

See this fiddle for a working example.

Post a Comment for "Encoding Problems With Xdocument Xelement When Using Replacewith Method"