0

I am using the following:

XElement select = new XElement("select");
XElement optGroup = null;

if (topics.Count() > 0) {
    optGroup = new XElement("optgroup", new XAttribute("label", "Admin"));
    optGroup.Add(new XElement("option", new XAttribute("value", "99"), new XText("Select Topic")));
    optGroup.Add(new XElement("option", new XAttribute("value", "00"), new XText("All Topics")));
    select.Add(optGroup);
    foreach (var topic in topics) {
        // skip root topic
        if (!topic.RowKey.EndsWith("0000")) {
            // optgroup
            if (topic.RowKey.EndsWith("00")) {
                optGroup = new XElement("optgroup", new XAttribute("label", topic.Title));
                select.Add(optGroup);
            }
                // option
            else if (optGroup != null) {
                optGroup.Add(new XElement("option", new XAttribute("value", topic.RowKey), new XText(topic.Title)));
            }
        }
    }
} else {
    optGroup = new XElement("optgroup", new XAttribute("label", "Admin"));
    optGroup.Add(new XElement("option", new XAttribute("value", "88"), new XText("No Topics")));
}                     
return (select.ToString());

It processes rows in a variable and uses the data to create a <select> ... </select> element for HTML.

It works however what I need is the contents of the <select> and not the opening <select> and closing </select> elements. Is there some way I could make this just return contents and not the outer <select> ?

Richard
  • 106,783
  • 21
  • 203
  • 265
  • 1
    I think this may help: http://stackoverflow.com/questions/3793/best-way-to-get-innerxml-of-an-xelement – lesscode Jul 23 '12 at 17:19

2 Answers2

1

Just trim what you don't want from the result.

return (select.ToString().Replace("<select>","").Replace("</select>",""));
nunespascal
  • 17,584
  • 2
  • 43
  • 46
1

Add

using System.Xml.XPath;

to get access to the extension methods defined in the Extensions class in that namespace to get an XPathNavigator and its InnerXml property:

string inner = select.CreateNavigator().InnerXml;
Richard
  • 106,783
  • 21
  • 203
  • 265