Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Dynamic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Xml;
- using System.Xml.Linq;
- namespace ConsoleApplication4
- {
- class Program
- {
- static void Main(string[] args)
- {
- var p = new person("Einstein") { FName = "fnamehi" };
- var xml = p.ToXml();
- var str = xml.ToString();
- }
- }
- public class car
- {
- public string Make { get { return "nissan"; } }
- }
- public class person
- {
- public person(string ln)
- {
- LName = ln;
- address = new ExpandoObject();
- address.location1 = "1234 Fake st";
- address.zip = 63104;
- Friends = new List<string>() { "john", "franks" };
- car = new car();
- }
- public string FName { get; set; }
- public List<string> Friends { get; set; }
- public string LName { get; private set; }
- public int Length { get { return 6; } }
- private int age { get; set; }
- public dynamic address { get; set; }
- public object car { get; set; }
- }
- /// <summary>
- /// Extension methods for the dynamic object.
- /// </summary>
- public static class DynamicHelper
- {
- private static readonly Type _enumerableType = typeof(IEnumerable);
- private static readonly Type _stringType = typeof(string);
- private static readonly Type _charType = typeof (char);
- private static readonly Type[] _writeTypes = new[] { _stringType, typeof(DateTime), typeof(Enum), typeof(decimal), typeof(Guid) };
- /// <summary>
- /// Determines whether [is simple type] [the specified type].
- /// </summary>
- /// <param name="type">The type to check.</param>
- /// <returns>
- /// <c>true</c> if [is simple type] [the specified type]; otherwise, <c>false</c>.
- /// </returns>
- public static bool IsSimpleType(this Type type)
- {
- return type.IsPrimitive || _writeTypes.Contains(type);
- }
- /// <summary>
- /// Converts an anonymous type to an XElement.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <returns>Returns the object as it's XML representation in an XElement.</returns>
- public static XElement ToXml(this object input)
- {
- return input.ToXml(null);
- }
- /// <summary>
- /// Converts an anonymous type to an XElement.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="element">The element name.</param>
- /// <returns>Returns the object as it's XML representation in an XElement.</returns>
- public static XElement ToXml(this object input, string element)
- {
- if (input == null)
- {
- return null;
- }
- if (String.IsNullOrEmpty(element))
- {
- element = "object";
- }
- element = XmlConvert.EncodeName(element);
- var ret = new XElement(element);
- if (input != null)
- {
- var type = input.GetType();
- var props = type.GetProperties();
- var elements = from prop in props
- let interfaces = prop.PropertyType.GetInterfaces()
- let isEnumerable = interfaces.Contains(_enumerableType)
- && prop.PropertyType != _stringType
- && prop.PropertyType != _charType
- let propertyName = XmlConvert.EncodeName(prop.Name)
- let value = isEnumerable ? GetEnumerableElement(prop, (IEnumerable)prop.GetValue(input, null))
- : (prop.PropertyType.IsSimpleType() ? new XElement(propertyName, prop.GetValue(input, null))
- : prop.GetValue(input, null).ToXml(propertyName))
- where value != null
- select value;
- ret.Add(elements);
- }
- return ret;
- }
- private static XElement GetEnumerableElement(PropertyInfo info, IEnumerable input)
- {
- var name = XmlConvert.EncodeName(info.Name);
- XElement rootElement = new XElement(name);
- foreach (var ob in input)
- {
- XElement childElement = ob.GetType().IsSimpleType() ? new XElement(name + "Child", ob) : ob.ToXml();
- rootElement.Add(childElement);
- }
- return rootElement;
- }
- }
- }
Add Comment
Please, Sign In to add comment