• <tfoot id='xpMAj'></tfoot>

    1. <legend id='xpMAj'><style id='xpMAj'><dir id='xpMAj'><q id='xpMAj'></q></dir></style></legend>

      <small id='xpMAj'></small><noframes id='xpMAj'>

        <bdo id='xpMAj'></bdo><ul id='xpMAj'></ul>
      1. <i id='xpMAj'><tr id='xpMAj'><dt id='xpMAj'><q id='xpMAj'><span id='xpMAj'><b id='xpMAj'><form id='xpMAj'><ins id='xpMAj'></ins><ul id='xpMAj'></ul><sub id='xpMAj'></sub></form><legend id='xpMAj'></legend><bdo id='xpMAj'><pre id='xpMAj'><center id='xpMAj'></center></pre></bdo></b><th id='xpMAj'></th></span></q></dt></tr></i><div id='xpMAj'><tfoot id='xpMAj'></tfoot><dl id='xpMAj'><fieldset id='xpMAj'></fieldset></dl></div>

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

        时间:2023-08-28

            <tbody id='0RyAM'></tbody>
              <i id='0RyAM'><tr id='0RyAM'><dt id='0RyAM'><q id='0RyAM'><span id='0RyAM'><b id='0RyAM'><form id='0RyAM'><ins id='0RyAM'></ins><ul id='0RyAM'></ul><sub id='0RyAM'></sub></form><legend id='0RyAM'></legend><bdo id='0RyAM'><pre id='0RyAM'><center id='0RyAM'></center></pre></bdo></b><th id='0RyAM'></th></span></q></dt></tr></i><div id='0RyAM'><tfoot id='0RyAM'></tfoot><dl id='0RyAM'><fieldset id='0RyAM'></fieldset></dl></div>
              • <bdo id='0RyAM'></bdo><ul id='0RyAM'></ul>
                <legend id='0RyAM'><style id='0RyAM'><dir id='0RyAM'><q id='0RyAM'></q></dir></style></legend>

                <small id='0RyAM'></small><noframes id='0RyAM'>

                  <tfoot id='0RyAM'></tfoot>
                1. 本文介绍了如何使用 C#/LINQ 将 XML 转换为 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下需要在服务器中转换为 JSON 的 XML 文件.最初我以为我会将它转换为字典,然后使用 JavaScriptSerializer 将其转换为 JSON,但由于每列可能有不同的值类型,我认为它不会起作用.以前有人在 C#/LINQ 中做过类似的事情吗?

                  I have the following XML file that I need to convert to JSON in the server. Initially I thought I would convert it to a Dictionary and then use the JavaScriptSerializer to turn it into JSON but since each column could have a different value type, I don't think it would work. Has anyone done something similar before in C#/LINQ?

                  我需要保留每列的值类型(布尔、字符串、整数).

                  I need to preserve the Value Types(Boolean, String, Integer) of each column.

                  如果我刚刚开始使用 XML,我将不胜感激.谢谢.

                  I would appreciate any advice on this as Im just starting to work with XML. Thanks.

                  <Columns>
                   <Column Name="key1" DataType="Boolean">True</Column>
                   <Column Name="key2" DataType="String">Hello World</Column>
                   <Column Name="key3" DataType="Integer">999</Column>
                  </Columns>
                  

                  推荐答案

                  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"}
                  

                  显然,这会将所有值都视为字符串.如果您想保留底层类型语义,您可以执行以下操作:

                  Obviously this treats all the values as strings. If you want to keep the underlying type semantics you could do the following:

                  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 类型之间进行转换:

                  And if you cannot modify the underlying XML structure you will need a custom function that will convert between your custom types and the underlying .NET type:

                  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)
                                  );
                          }
                      }
                  }
                  

                  这篇关于如何使用 C#/LINQ 将 XML 转换为 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:xml 文档异常中禁止的 DTD 下一篇:在 .net 中读取大型 XML 文档

                  相关文章

                  最新文章

                  <legend id='UW8tY'><style id='UW8tY'><dir id='UW8tY'><q id='UW8tY'></q></dir></style></legend>
                2. <tfoot id='UW8tY'></tfoot>
                  • <bdo id='UW8tY'></bdo><ul id='UW8tY'></ul>
                  <i id='UW8tY'><tr id='UW8tY'><dt id='UW8tY'><q id='UW8tY'><span id='UW8tY'><b id='UW8tY'><form id='UW8tY'><ins id='UW8tY'></ins><ul id='UW8tY'></ul><sub id='UW8tY'></sub></form><legend id='UW8tY'></legend><bdo id='UW8tY'><pre id='UW8tY'><center id='UW8tY'></center></pre></bdo></b><th id='UW8tY'></th></span></q></dt></tr></i><div id='UW8tY'><tfoot id='UW8tY'></tfoot><dl id='UW8tY'><fieldset id='UW8tY'></fieldset></dl></div>

                    1. <small id='UW8tY'></small><noframes id='UW8tY'>