久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      <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 class="qwawimqqmiuu" 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 轉(zhuǎn)換為 JSON?

        How to convert XML to JSON using C#/LINQ?(如何使用 C#/LINQ 將 XML 轉(zhuǎn)換為 JSON?)

      2. <tfoot id='xpMAj'></tfoot>
            <tbody id='xpMAj'></tbody>

          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 class="qwawimqqmiuu" id='xpMAj'><tfoot id='xpMAj'></tfoot><dl id='xpMAj'><fieldset id='xpMAj'></fieldset></dl></div>

                  本文介紹了如何使用 C#/LINQ 將 XML 轉(zhuǎn)換為 JSON?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  我有以下需要在服務(wù)器中轉(zhuǎn)換為 JSON 的 XML 文件.最初我以為我會(huì)將它轉(zhuǎn)換為字典,然后使用 JavaScriptSerializer 將其轉(zhuǎn)換為 JSON,但由于每列可能有不同的值類(lèi)型,我認(rèn)為它不會(huì)起作用.以前有人在 C#/LINQ 中做過(guò)類(lèi)似的事情嗎?

                  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?

                  我需要保留每列的值類(lèi)型(布爾、字符串、整數(shù)).

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

                  如果我剛剛開(kāi)始使用 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);
                      }
                  }
                  

                  產(chǎn)生:

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

                  顯然,這會(huì)將所有值都視為字符串.如果您想保留底層類(lèi)型語(yǔ)義,您可以執(zhí)行以下操作:

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

                  產(chǎn)生:

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

                  如果您無(wú)法修改底層 XML 結(jié)構(gòu),您將需要一個(gè)自定義函數(shù),該函數(shù)將在您的自定義類(lèi)型和底層 .NET 類(lèi)型之間進(jìn)行轉(zhuǎn)換:

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

                  這篇關(guān)于如何使用 C#/LINQ 將 XML 轉(zhuǎn)換為 JSON?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Ignore whitespace while reading XML(讀取 XML 時(shí)忽略空格)
                  XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                  Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標(biāo)簽的 XML)
                  Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                  delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                  Parse malformed XML(解析格式錯(cuò)誤的 XML)
                  <legend id='UW8tY'><style id='UW8tY'><dir id='UW8tY'><q id='UW8tY'></q></dir></style></legend>

                  <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 class="qwawimqqmiuu" id='UW8tY'><tfoot id='UW8tY'></tfoot><dl id='UW8tY'><fieldset id='UW8tY'></fieldset></dl></div>

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

                            <tbody id='UW8tY'></tbody>
                          • 主站蜘蛛池模板: 国产精品夜间视频香蕉 | 久久久久久久一区 | 欧美日韩久久 | 国产91久久精品一区二区 | 午夜视频在线观看一区二区 | 成人国产精品久久 | 欧美成人精品一区 | 日韩精品一区二区三区高清免费 | 99re在线视频| 国产精品国产a级 | 96久久久久久 | 一区二区高清 | 久久精品国产一区二区三区不卡 | 国产网站在线播放 | 久久这里只有精品首页 | 久久久精彩视频 | 视频一区 亚洲 | 黄网免费看 | 午夜精品久久久久久不卡欧美一级 | 国产999精品久久久久久 | 91一区二区三区在线观看 | 九九久久精品视频 | 国产精品99久久久久久久久久久久 | 看片网站在线 | 欧美日韩亚洲一区 | 欧美精品福利视频 | 激情在线视频网站 | 中文字幕在线第二页 | 精品在线| 久久久久久亚洲精品不卡 | 国产成人免费网站 | va在线 | 欧美精品tv| 亚洲欧美综合精品久久成人 | 国产精品高潮呻吟久久av黑人 | 99精品一区二区 | 亚洲美女一区 | 欧美性吧| 午夜免费视频 | 中文字幕久久久 | 精品久久av |