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