using System.Data;
using System.Xml.Serialization;
using System.Text;
using System.Xml;

namespace CallCenterApi.Common
{
    public static class XmlFormatHelper
    {
        /// <summary>
        /// DataSet转XML字符串
        /// </summary>
        /// <param name="Ds"></param>
        /// <returns></returns>
        public static string DataSetToXml(DataSet Ds)
        { 
            string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
            xmlStr += "<NewDataSet>";
            for (int i = 0; i < Ds.Tables.Count; i++)
            {
                xmlStr += GetTableSetXml("NewTable" + i, Ds.Tables[i]);
            }
            xmlStr += "</NewDataSet>";
            return xmlStr;
        }

        /// <summary>
        /// DataSet转XML字符串
        /// </summary>
        /// <param name="Ds"></param>
        /// <returns></returns>
        public static string DataSetToXmlTable(DataSet Ds)
        {
            string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
            xmlStr += "<NewDataSet>";
            for (int i = 0; i < Ds.Tables.Count; i++)
            {
                xmlStr += GetTableSetXmlTable("NewTable" + i, Ds.Tables[i]);
            }
            xmlStr += "</NewDataSet>";
            return xmlStr;
        }

        public static string GetTableSetXmlTable(string tableName, DataTable table)
        {
            string str = string.Empty;
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    str += "<" + tableName + ">";
                    for (int j = 0; j < table.Columns.Count; j++)
                    {
                        string clName = table.Columns[j].ColumnName;
                        str += "<" + clName + ">" + table.Rows[i][clName] + "</" + clName + ">";
                    }
                    str += "</" + tableName + ">";
                }
            }
            else
            {
                str += "<" + tableName + ">";
                str += "<tablerows>0</tablerows>";
                str += "</" + tableName + ">";
            }
            return str;
        }

        public static string GetTableSetXml(string tableName, DataTable table)
        {
            string str = string.Empty;
            for (int i = 0; i < table.Rows.Count; i++)
            {
                str += "<" + tableName + ">";
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    string clName = table.Columns[j].ColumnName;
                    str += "<" + clName + ">" + table.Rows[i][clName] + "</" + clName + ">";
                }
                str += "</" + tableName + ">";
            }
            return str;
        }

        /// <summary>
        /// 把字符串转成XML格式,字符串格式：{"  ID:1,User:aaa  "}，仅限根目录下的子节点，附加根节点"Root"
        /// </summary>
        /// <param name="Souce"></param>
        /// <returns></returns>
        public static string StringToXml(string Souce)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
            str.Append("<Root>\r\n");
            string[] _list = Souce.Split(',');
            for (int i = 0; i < _list.Length; i++)
            {
                string[] _temp = _list[i].Split(':');
                str.Append("<" + _temp[0] + ">");
                str.Append(_temp[1]);
                str.Append("</" + _temp[0] + ">\r\n");
            }
            str.Append("</Root>");
            return str.ToString();
        }

        public static string ToXml<T>(T item)
        {
            XmlSerializer serializer = new XmlSerializer(item.GetType());
            StringBuilder sb = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(sb))
            {
                serializer.Serialize(writer, item);
                return sb.ToString();
            }
        }

        public static string ApiGetDataSetToXml(DataSet Ds)
        {
            string str = string.Empty;
            str += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
            str += "<document>";
            str += "<site_name><![CDATA[]]></site_name>";
            str += "<site_url><![CDATA[http://www.com]]></site_url>";
            str += "<site_logo><![CDATA[http://www.com/images/logosd.gif]]></site_logo>";
            str += "<update_time><![CDATA[" + Ds.Tables[0].Rows[0]["update_time"] + "]]></update_time>";
            str += "<items>";
            if (Ds.Tables.Count > 1)
            {
                var table = Ds.Tables[1];
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    str += "<item>";
                    for (int j = 0; j < table.Columns.Count; j++)
                    {
                        string clName = table.Columns[j].ColumnName;
                        str += "<" + clName + "><![CDATA[" + table.Rows[i][clName] + "]]></" + clName + ">";
                    }
                    str += "</item>";
                }
            }
            str += "</items>";
            str += "</document>";
            return str;
        }
    }
}
