0

I have an XDocument object which contains XHTML and I am looking to add ABBR elements into the string. I have a List that I am looping through to look for values which need to be wrapped in ABBR elements.

Lets say I have an XElement which contains XHTML like so:

<p>Some text will go here</p>

I need to adjust the value of the XElement to look like this:

<p>Some text <abbr title="Will Description">will</abbr> go here</p>

How do I do this?

UPDATE:

I am wrapping the value "will" with the HTML element ABBR.

This is what I have so far:

        // Loop through them
        foreach (XElement xhtmlElement in allElements)
        {
            // Don't process this element if it has child elements as they
            // will also be processed through here.
            if (!xhtmlElement.Elements().Any())
            {
                string innerText = GetInnerText(xhtmlElement);

                foreach (var abbrItem in AbbreviationItems)
                {
                    if (innerText.ToLower().Contains(abbrItem.Description.ToLower()))
                    {
                        var abbrElement = new XElement("abbr", 
                            new XAttribute("title", abbrItem.Abbreviation),
                            abbrItem.Description);

                        innerText = Regex.Replace(innerText, abbrItem.Description, abbrElement.ToString(),
                                                  RegexOptions.IgnoreCase);

                        xhtmlElement.Value = innerText;
                    }
                }
            }
        }

The problem with this approach is that when I set the XElement Value property, it is encoding the XML tags (correctly treating it as a string rather than XML).

rf_wilson
  • 1,562
  • 5
  • 32
  • 44
  • There's not enough information here. Why is "will" wrapped? What have you tried so far? – dav_i Mar 13 '15 at 14:15
  • How much do you care about preserving the exact whitespace between words etc? – Jon Skeet Mar 13 '15 at 14:18
  • There are several parts to this question: (1) Given a string, find known abbreviations within it. (2) Given an abbreviation, find its description. (3) Given an XElement containing text, replace a portion of the text with an XElement. Which part do you need help with? – Michael Liu Mar 13 '15 at 14:22
  • @Michael Liu, part 3 please. – rf_wilson Mar 13 '15 at 14:22

2 Answers2

0

If innerText contains the right XML you can try the following:

xhtmlElement.Value = XElement.Parse(innerText);

instead of

xhtmlElement.Value = innerText;
MarkusE
  • 545
  • 4
  • 20
0

you can :

  • change the element value first to string,
  • edit and replace the previous element with the xmltag,
  • and then replace old value with the new value.

this might what you're looking for:

var element = new XElement("div");
var xml = "<p>Some text will go here</p>";
element.Add(XElement.Parse(xml));

//Element to replace/rewrite
XElement p = element.Element("p");
var value = p.ToString();
var newValue = value.Replace("will", "<abbr title='Will Description'>will</abbr>");
p.ReplaceWith(XElement.Parse(newValue));
Fedri Qrueger
  • 641
  • 5
  • 23