栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用C#/ LINQ将XML转换为JSON?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用C#/ LINQ将XML转换为JSON?

using System;using System.Linq;using System.Web.script.Serialization;using System.Xml.Linq;class Program{    static void Main()    {        var xml =         @"<Columns>          <Column Name=""key1"" DataType=""Boolean"">True</Column>          <Column Name=""key2"" DataType=""String"">Hello World</Column>          <Column Name=""key3"" DataType=""Integer"">999</Column>        </Columns>";        var dic = Xdocument .Parse(xml) .Descendants("Column") .ToDictionary(     c => c.Attribute("Name").Value,      c => c.Value );        var json = new JavascriptSerializer().Serialize(dic);        Console.WriteLine(json);    }}

产生:

{"key1":"True","key2":"Hello World","key3":"999"}

显然,这会将所有值视为字符串。如果要保留基础类型语义,可以执行以下操作:

using System;using System.Linq;using System.Web.script.Serialization;using System.Xml.Linq;class Program{    static void Main()    {        var xml =         @"<Columns>          <Column Name=""key1"" DataType=""System.Boolean"">True</Column>          <Column Name=""key2"" DataType=""System.String"">Hello World</Column>          <Column Name=""key3"" DataType=""System.Int32"">999</Column>        </Columns>";        var dic = Xdocument .Parse(xml) .Descendants("Column") .ToDictionary(     c => c.Attribute("Name").Value,      c => Convert.ChangeType(         c.Value,         typeof(string).Assembly.GetType(c.Attribute("DataType").Value, true)     ) );        var json = new JavascriptSerializer().Serialize(dic);        Console.WriteLine(json);    }}

产生:

{"key1":true,"key2":"Hello World","key3":999}

而且,如果您不能修改基础XML结构,则需要一个自定义函数,该函数将在您的自定义类型和基础.NET类型之间进行转换:

using System;using System.Linq;using System.Web.script.Serialization;using System.Xml.Linq;class Program{    static void Main()    {        var xml =         @"<Columns>          <Column Name=""key1"" DataType=""Boolean"">True</Column>          <Column Name=""key2"" DataType=""String"">Hello World</Column>          <Column Name=""key3"" DataType=""Integer"">999</Column>        </Columns>";        var dic = Xdocument .Parse(xml) .Descendants("Column") .ToDictionary(     c => c.Attribute("Name").Value,      c => Convert.ChangeType(         c.Value,          GetType(c.Attribute("DataType").Value)     ) );        var json = new JavascriptSerializer().Serialize(dic);        Console.WriteLine(json);    }    private static Type GetType(string type)    {        switch (type)        { case "Integer":     return typeof(int); case "String":     return typeof(string); case "Boolean":     return typeof(bool); // TODO: add any other types that you want to support default:     throw new NotSupportedException(         string.Format("The type {0} is not supported", type)     );        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/485418.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号