XmlDocument是WebService方法的一種常用的傳回型別。
public static XmlDocument CreateXmlDocument(string function,bool b) { XmlDocument xmlDoc = new XmlDocument(); //建立Xml的定義聲明 //XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null); //xmlDoc.AppendChild(dec); //建立根節點 XmlElement root = xmlDoc.CreateElement("Description"); xmlDoc.AppendChild(root); XmlElement name = xmlDoc.CreateElement("Name"); name.InnerText = ""; XmlElement value = xmlDoc.CreateElement("Value"); value.InnerText = ""; switch (function) { case "select"://查詢操作 if (b) { name.InnerText = "0"; value.InnerText = "查詢操作成功!"; } else { name.InnerText = "1"; value.InnerText = "查詢操作失敗,請稍後再試!"; } break; } root.AppendChild(name); root.AppendChild(value); return xmlDoc; }
對應的XML樣式如下:
<?xml version="1.0" encoding="utf-8" ?> <Description><Name>1</Name> <Value>查詢操作失敗,請稍後再試!</Value> </Description>