發(fā)布時(shí)間:2020-08-06 15:19 作者:獨(dú)孤劍 閱讀:2060
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Xml; namespace ToolDemo { class Program { static void Main(string[] args) { string xml = @" <xml> <UserName><![CDATA[Administrator]]></UserName> <Age><![CDATA[18]]></Age> </xml>"; Hashtable ht = XmlToHashtable(xml); Console.WriteLine("UserName: {0}", (string)ht["UserName"]); Console.WriteLine("Age: {0}", (string)ht["Age"]); /* 運(yùn)行結(jié)果 UserName: Administrator Age: 18 */ Console.Read(); } /// <summary> /// 將Xml文本轉(zhuǎn)換成Hashtable /// </summary> /// <param name="xml"></param> /// <returns></returns> public static Hashtable XmlToHashtable(string xml) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); XmlNode xmlNode = xmlDoc.ChildNodes[0]; Hashtable ht = new Hashtable(); if (xmlNode.ChildNodes.Count > 0) { foreach (XmlNode xn in xmlNode.ChildNodes) { ht.Add(xn.Name, xn.InnerText); } } return ht; } /// <summary> /// 將Xml對(duì)象轉(zhuǎn)換成Hashtable /// </summary> /// <param name="xmlDoc"></param> /// <returns></returns> public static Hashtable XmlToHashtable(XmlDocument xmlDoc) { XmlNode xmlNode = xmlDoc.ChildNodes[0]; Hashtable ht = new Hashtable(); if (xmlNode.ChildNodes.Count > 0) { foreach (XmlNode xn in xmlNode.ChildNodes) { ht.Add(xn.Name, xn.InnerText); } } return ht; } } }
微信打賞, 微信掃一掃
支付寶打賞, 支付寶掃一掃
如果文章對(duì)您有幫助,歡迎給作者打賞