c# XML转JSON、JSON转XML,object转JSON

        public static string XmlTojson(this string s, string r = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>") {

            s = s.Replace(r, "");
            var doc = new XmlDocument();
            doc.LoadXml(s);
            return JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.None, true);
        }

        public static string JsonToxml(this string s, string r = "root") {
            var doc = JsonConvert.DeserializeXmlNode(s, r);

            using (var stream = new MemoryStream()) {
                var writer = new XmlTextWriter(stream, null) { Formatting = System.Xml.Formatting.Indented };
                doc.Save(writer);

                using (var sr = new StreamReader(stream, Encoding.UTF8)) {
                    stream.Position = 0;
                    return sr.ReadToEnd();
                }
            }

        }

        public static string Json(this object o) {
            return new JavaScriptSerializer().Serialize(o);
        }

        public static object JsonParse(this string s) {
            return s.Trim().IsEmpty() ? new object() : new JavaScriptSerializer().Deserialize<object>(s);
        }

        public static bool IsJson(this string s) { try { JsonParse(s); return true; } catch { return false; } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 C# 程序,可以将 Excel 文件换为多种格式,如 CSV、XMLJSON 等。程序使用了 Microsoft.Office.Interop.Excel 库来读取 Excel 文件,使用 Newtonsoft.Json 库来序列化为 JSON 格式。 ```csharp using System; using System.IO; using System.Linq; using Microsoft.Office.Interop.Excel; using Newtonsoft.Json; namespace ExcelConverter { class Program { static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Usage: ExcelConverter.exe <input_file> <output_format>"); return; } string inputFile = args[0]; string outputFormat = args[1]; Application excelApp = new Application(); Workbook workbook = excelApp.Workbooks.Open(inputFile); Worksheet worksheet = workbook.ActiveSheet; Range range = worksheet.UsedRange; switch (outputFormat) { case "csv": ConvertToCsv(range); break; case "xml": ConvertToXml(range); break; case "json": ConvertToJson(range); break; default: Console.WriteLine($"Unsupported output format: {outputFormat}"); break; } workbook.Close(); excelApp.Quit(); } static void ConvertToCsv(Range range) { string csv = ""; for (int row = 1; row <= range.Rows.Count; row++) { string[] values = Enumerable.Range(1, range.Columns.Count) .Select(col => range.Cells[row, col].Value.ToString()) .ToArray(); csv += string.Join(",", values) + Environment.NewLine; } Console.WriteLine(csv); } static void ConvertToXml(Range range) { string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + Environment.NewLine; xml += "<table>" + Environment.NewLine; for (int row = 1; row <= range.Rows.Count; row++) { xml += "<row>" + Environment.NewLine; for (int col = 1; col <= range.Columns.Count; col++) { string cellValue = range.Cells[row, col].Value.ToString(); xml += $"<col{col}>{cellValue}</col{col}>" + Environment.NewLine; } xml += "</row>" + Environment.NewLine; } xml += "</table>" + Environment.NewLine; Console.WriteLine(xml); } static void ConvertToJson(Range range) { dynamic table = new System.Dynamic.ExpandoObject(); table.rows = Enumerable.Range(1, range.Rows.Count) .Select(row => { dynamic rowData = new System.Dynamic.ExpandoObject(); for (int col = 1; col <= range.Columns.Count; col++) { string colName = $"col{col}"; string cellValue = range.Cells[row, col].Value.ToString(); ((IDictionary<string, object>)rowData)[colName] = cellValue; } return rowData; }) .ToList(); string json = JsonConvert.SerializeObject(table, Formatting.Indented); Console.WriteLine(json); } } } ``` 该程序使用命令行参数来指定输入文件和输出格式,例如: ``` ExcelConverter.exe input.xlsx csv ``` 在上述示例中,程序将 `input.xlsx` 文件换为 CSV 格式,并将结果输出到控制台。你可以根据需要修改程序代码,以支持更多的输出格式和选项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值