如何使用C#创建没有包含元素的XML?

如何使用C#创建没有包含元素的XML?,c#,linq-to-xml,C#,Linq To Xml,我正在使用以下工具: 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", "9

我正在使用以下工具:

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());
它处理变量中的行,并使用数据创建
元素


但是它可以工作,我需要的是
的内容,而不是开头
和结尾
元素。有没有办法让它只返回内容而不返回外部的

只从结果中删除您不想要的内容

return (select.ToString().Replace("<select>","").Replace("</select>",""));
return(选择.ToString().Replace(“,”).Replace(“,”);
添加

要访问在该命名空间中的类中定义的扩展方法以获取及其属性,请执行以下操作:

string inner = select.CreateNavigator().InnerXml;
我认为这可能有助于:
string inner = select.CreateNavigator().InnerXml;