C#開發之生成XmlDocument用來返回錯誤提示信息

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>



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章