日期:2014-05-18  浏览次数:20488 次

jason数据如何转成XML数据?
jason数据格式如何转成XML数据格式?。NET有这方面的函数吗?

------解决方案--------------------
C# code

XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(xml), XmlDictionaryReaderQuotas.Max);
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(reader);

------解决方案--------------------
C# code
public static string DataTableToJSON(DataTable table)
        {
            StringBuilder json = new StringBuilder();
            json.Append("[");
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow row = table.Rows[i];

                // json object
                json.Append("{");
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    string columnName = table.Columns[j].ColumnName;
                    string columnType = table.Columns[j].DataType.Name;

                    // json field
                    if (columnType == "Int32" || columnType == "Int16" || columnType == "Decimal")
                    {
                        // don't surround numbers with quotes
                        json.AppendFormat("\"{0}\":\"{1}\"", columnName, row.IsNull(columnName) ? "" : row[columnName]);
                    }
                    else if (columnType == "Boolean")
                    {
                        // make the bool value lowercase
                        json.AppendFormat("\"{0}\":{1}", columnName, row.IsNull(columnName) ? "" : row[columnName].ToString().ToLower());
                    }
                    else
                    {
                        // everything else gets quotes around the data
                        json.AppendFormat("\"{0}\":\"{1}\"", columnName, row[columnName]);
                    }

                    if (j < table.Columns.Count - 1) json.Append(","); // add comma if not last column
                }
                json.Append("}");

                if (i < table.Rows.Count - 1) json.Append(","); // add comma if not last row
            }
            json.Append("]");
            return json.ToString();
        }