總結一個xml操作的類:
/// <summary>
/// 創建XML文檔
/// </summary>
/// <param name="name">根節點名稱</param>
/// <param name="type">根節點的一個屬性值</param>
/// <returns></returns>
/// moss中調用方法:創建的文件如果要存到moss的文檔庫中,則:
/// XmlDocument doc = XmlOperate.CreateXmlDocument("project", "T");
/// 在此可嵌入增加子節點方法,如AddTaskNode(taskObj, ref doc); ..
/// byte[] fileContent = Encoding.UTF8.GetBytes(doc.OuterXml);
/// folder.Files.Add("name.xml", fileContent, true);
/// web.Update();
/// .net中調用方法:寫入文件中,則:
/// document = XmlOperate.CreateXmlDocument("sex", "sexy");
/// document.Save("c:/bookstore.xml");
public static XmlDocument CreateXmlDocument(string name, string type)
{
XmlDocument doc = null;
XmlElement rootEle = null;
try
{
doc = new XmlDocument();
doc.LoadXml("<" + name + "/>");
rootEle = doc.DocumentElement;
rootEle.SetAttribute("type", type);
}
catch (Exception er)
{
throw er;
}
return doc;
}
/// <summary>
/// 在根節點下增加子元素