I’ve got the following XML passed as an IQueryable to a method, i.e.
XML:
<body>
<p>Some Text</p>
<p>Some Text</p>
<pullquote>This is pullquote</pullquote>
<p>Some Text</p>
<p>Some Text</p>
<body>
Method:
public static string CreateSection(IQueryable<XElement> section)
{
var bodySection = from b in articleSection.Descendants("body")
select b;
//Do replacement here.
var bodyElements = bodySection.Descendants();
StringBuilder paragraphBuilder = new StringBuilder();
foreach (XElement paragraph in bodyElements)
{
paragraphBuilder.Append(paragraph.ToString());
}
return paragraphBuilder.ToString();
}
What I want to accomplish is to replace the <pullquote> with a <p> (and maybe add attributes).
My problem is not the actual replacement (XElement.ReplaceWith()) but the fact that after the replacement the changes does not reflect the bodySection variable that gets used by the StringBuilder.
How would I go about getting this to work?
You haven’t really shown enough code – in particular, you haven’t shown the replacement code you’re trying to use.
However, I suspect the main problem is that
bodySectionis a query. Every time you use it, it will querysectionagain – and if that’s pulling information from the database, then so be it. You may find that this is all that’s required to make it do what you want:That way you’ve then got the body sections in memory, and any time you use
bodySectionyou’ll be using the same collection of objects, rather than querying again.