我正在用C#創建XML。我想追加item
至content
。我創建了一個與CreateItem
XmlNode,但我似乎無法將其追加到contentElement
。XmlNode.AppendChild失敗
XmlDocument doc = new XmlDocument();
XmlNode contentElement = doc.CreateElement("content");
doc.AppendChild(contentElement);
contentElement.AppendChild(CreateItem);
public XmlNode CreateItem(XmlDocument doc, string hint, string type, string title, string value) {
XmlNode item = doc.CreateElement("item");
XmlAttribute Hint = doc.CreateAttribute("Hint");
Hint.Value = hint;
XmlAttribute Type = doc.CreateAttribute("Type");
Type.Value = type;
item.Attributes.Append(Hint);
item.Attributes.Append(Type);
XmlNode tit = doc.CreateElement("Title");
tit.InnerText = title;
item.AppendChild(tit);
XmlNode val = doc.CreateElement("Value");
val.InnerText = value;
item.AppendChild(val);
return item;
}
什麼是'CreteItem',它在哪裏定義? –
public xmlNode,line 5 – Eleanor
出於興趣,是否有任何理由不能使用LINQ to XML?這通常是一個*多*更好的API ... –