C#將XML轉換成JSON轉換XML

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   sp   

原文: C#將XML轉換成JSON轉換XML

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using Newtonsoft.Json;  namespace JSonConverter{    class Program    {        static void Main(string[] args)        {            string xml = "<Test><Name>Test class</Name><X>100</X><Y>200</Y></Test>";              XmlDocument doc = new XmlDocument();            doc.LoadXml(xml);            string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);              Console.WriteLine("XML -> JSON: {0}", json);            Console.ReadLine();                      }    }}

json2xml

 預定義的Json字串如上

  同理調用Json.Net類庫中的方法

  XmlDocument doc1 = JsonConvert.DeserializeXmlNode(json);

  Console.WriteLine(doc1.OuterXml);


XmlDocument doc2 = JsonConvert.DeserializeXmlNode(json1);

  Console.WriteLine(doc2.OuterXml);

http://dotnet.chinaitlab.com/CSharp/927201.html

http://www.tuicool.com/articles/n2Uzya

 XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(sJson), XmlDictionaryReaderQuotas.Max);XmlDocument doc = new XmlDocument();doc.Load(reader);收藏一下吧 以後萬一用的到我試了一下,json字串需要把鍵加"才行如:{Name:"aaa",Value:1}  這裡Name和Value是鍵這樣寫法轉換的時候報錯需要寫成{"Name":"aaa","Value":1}  這個是第二種方法,這個鍵加不加"都正常翻譯  /// <summary>        /// json字串轉換為Xml對象        /// </summary>        /// <param name="sJson"></param>        /// <returns></returns>        public static XmlDocument Json2Xml(string sJson)        {            //XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(sJson), XmlDictionaryReaderQuotas.Max);            //XmlDocument doc = new XmlDocument();            //doc.Load(reader);            JavaScriptSerializer oSerializer = new JavaScriptSerializer();            Dictionary<string, object> Dic = (Dictionary<string, object>)oSerializer.DeserializeObject(sJson);            XmlDocument doc = new XmlDocument();            XmlDeclaration xmlDec;            xmlDec = doc.CreateXmlDeclaration("1.0", "gb2312", "yes");            doc.InsertBefore(xmlDec, doc.DocumentElement);            XmlElement nRoot = doc.CreateElement("root");            doc.AppendChild(nRoot);            foreach (KeyValuePair<string, object> item in Dic)            {                XmlElement element = doc.CreateElement(item.Key);                KeyValue2Xml(element, item);                nRoot.AppendChild(element);            }            return doc;        }        private static void KeyValue2Xml(XmlElement node, KeyValuePair<string, object> Source)        {            object kValue = Source.Value;            if (kValue.GetType() == typeof(Dictionary<string, object>))            {                foreach (KeyValuePair<string, object> item in kValue as Dictionary<string, object>)                {                    XmlElement element = node.OwnerDocument.CreateElement(item.Key);                    KeyValue2Xml(element, item);                    node.AppendChild(element);                }            }            else if (kValue.GetType() == typeof(object[]))            {                object[] o = kValue as object[];                for (int i = 0; i < o.Length; i++)                {                    XmlElement xitem = node.OwnerDocument.CreateElement("Item");                    KeyValuePair<string, object> item = new KeyValuePair<string, object>("Item", o[i]);                    KeyValue2Xml(xitem, item);                    node.AppendChild(xitem);                }                               }            else            {                XmlText text = node.OwnerDocument.CreateTextNode(kValue.ToString());                node.AppendChild(text);            }        } 


C#將XML轉換成JSON轉換XML

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.