Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Linq;
- using System.Web.Script.Serialization;
- using System.Xml.Linq;
- class Program
- {
- static void Main()
- {
- var json = new JavaScriptSerializer().Serialize(GetXmlData(XElement.Parse("<xml/>")));
- }
- private static Dictionary<string, object> GetXmlData(XElement xml)
- {
- var attr = xml.Attributes().ToDictionary(d => d.Name.LocalName, d => (object)d.Value);
- if (xml.HasElements) attr.Add("_value", xml.Elements().Select(e => GetXmlData(e)));
- else if (!xml.IsEmpty) attr.Add("_value", xml.Value);
- return new Dictionary<string, object> { { xml.Name.LocalName, attr } };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement