0

私は以下を使用しています:

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());

変数内の行を処理し、データを使用し<select> ... </select>て HTML の要素を作成します。

それは機能しますが、必要なのは開始要素と終了要素<select>ではなく、の内容です。これをアウターではなくコンテンツを返すようにする方法はありますか?<select></select><select>

4

2 に答える 2

1

追加

using System.Xml.XPath;

Extensionsその名前空間のクラスで定義されている拡張メソッドにアクセスして、XPathNavigatorとそのInnerXmlプロパティを取得するには、次のようにします。

string inner = select.CreateNavigator().InnerXml;
于 2012-07-23T17:21:47.343 に答える
1

結果から不要なものを切り取るだけです。

return (select.ToString().Replace("<select>","").Replace("</select>",""));
于 2012-07-23T17:21:12.810 に答える