I’m working on a C# application that pulls apart two Xml documents, merges some of their tag content, and produces a third Xml document. I’m faced with a situation where I need to extract the value of one tag, including inner tags, and transfer it to another tag. I started out doing something like this:
var summaryElement = elementExternal.Element("summary");
var summaryValue = (string)summaryElement;
var summaryValueClean = ElementValueClean(summaryValue);
var result = new XElement("para", summaryValueClean)
Where the ElementValueClean function removes extraneous white space.
This works satisfactorily if the value of the summary tag contains only text. The rub comes when the summary tag contains child elements like:
<summary>
Notifies the context that a new link exists between the <paramref name="source" /> and <paramref name="target" /> objects
and that the link is represented via the source.<paramref name="sourceProperty" /> which is a collection.
The context adds this link to the set of newly created links to be sent to
the data service on the next call to SaveChanges().
</summary>
I would like to produce something like this:
<para>
Notifies the context that a new link exists between the <paramref name="source" /> and <paramref name="target" /> objects
and that the link is represented via the source.<paramref name="sourceProperty" /> which is a collection.
The context adds this link to the set of newly created links to be sent to
the data service on the next call to SaveChanges().
</para>
There are roughly a dozen possible embedded tags that could appear across my catalog of source tags whose content I must merge into output tags. So I would like a C# solution that I can generalize. However, an Xslt transform that I can apply to Xml fragment to produce the Xml fragment would work for me also if it is simple enough. My Xslt skills have diminished from disuse.
You could update the
ElementValueClean()function to support inline nodes and accept an Element instead of its string value:An XSLT to rewrap the element is really simple, but I think a C# solution still makes more sense because you already have a usable C# text cleanup solution.
Or you could do the whole thing in XSLT, including text cleanup. It’s not clear what that function does, but this is how you’d start it in XSLT: