Guest User

Untitled

a guest
Jun 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Dynamic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Xml;
  9. using System.Xml.Linq;
  10.  
  11. namespace ConsoleApplication4
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. var p = new person("Einstein") { FName = "fnamehi" };
  18. var xml = p.ToXml();
  19. var str = xml.ToString();
  20. }
  21. }
  22. public class car
  23. {
  24. public string Make { get { return "nissan"; } }
  25. }
  26. public class person
  27. {
  28. public person(string ln)
  29. {
  30. LName = ln;
  31. address = new ExpandoObject();
  32. address.location1 = "1234 Fake st";
  33. address.zip = 63104;
  34. Friends = new List<string>() { "john", "franks" };
  35. car = new car();
  36. }
  37.  
  38. public string FName { get; set; }
  39. public List<string> Friends { get; set; }
  40. public string LName { get; private set; }
  41. public int Length { get { return 6; } }
  42. private int age { get; set; }
  43. public dynamic address { get; set; }
  44. public object car { get; set; }
  45. }
  46.  
  47. /// <summary>
  48. /// Extension methods for the dynamic object.
  49. /// </summary>
  50. public static class DynamicHelper
  51. {
  52. private static readonly Type _enumerableType = typeof(IEnumerable);
  53. private static readonly Type _stringType = typeof(string);
  54. private static readonly Type _charType = typeof (char);
  55. private static readonly Type[] _writeTypes = new[] { _stringType, typeof(DateTime), typeof(Enum), typeof(decimal), typeof(Guid) };
  56. /// <summary>
  57. /// Determines whether [is simple type] [the specified type].
  58. /// </summary>
  59. /// <param name="type">The type to check.</param>
  60. /// <returns>
  61. /// <c>true</c> if [is simple type] [the specified type]; otherwise, <c>false</c>.
  62. /// </returns>
  63. public static bool IsSimpleType(this Type type)
  64. {
  65. return type.IsPrimitive || _writeTypes.Contains(type);
  66. }
  67.  
  68. /// <summary>
  69. /// Converts an anonymous type to an XElement.
  70. /// </summary>
  71. /// <param name="input">The input.</param>
  72. /// <returns>Returns the object as it's XML representation in an XElement.</returns>
  73. public static XElement ToXml(this object input)
  74. {
  75. return input.ToXml(null);
  76. }
  77.  
  78. /// <summary>
  79. /// Converts an anonymous type to an XElement.
  80. /// </summary>
  81. /// <param name="input">The input.</param>
  82. /// <param name="element">The element name.</param>
  83. /// <returns>Returns the object as it's XML representation in an XElement.</returns>
  84. public static XElement ToXml(this object input, string element)
  85. {
  86. if (input == null)
  87. {
  88. return null;
  89. }
  90.  
  91. if (String.IsNullOrEmpty(element))
  92. {
  93. element = "object";
  94. }
  95.  
  96. element = XmlConvert.EncodeName(element);
  97. var ret = new XElement(element);
  98.  
  99. if (input != null)
  100. {
  101. var type = input.GetType();
  102. var props = type.GetProperties();
  103.  
  104. var elements = from prop in props
  105. let interfaces = prop.PropertyType.GetInterfaces()
  106. let isEnumerable = interfaces.Contains(_enumerableType)
  107. && prop.PropertyType != _stringType
  108. && prop.PropertyType != _charType
  109. let propertyName = XmlConvert.EncodeName(prop.Name)
  110. let value = isEnumerable ? GetEnumerableElement(prop, (IEnumerable)prop.GetValue(input, null))
  111. : (prop.PropertyType.IsSimpleType() ? new XElement(propertyName, prop.GetValue(input, null))
  112. : prop.GetValue(input, null).ToXml(propertyName))
  113. where value != null
  114. select value;
  115.  
  116. ret.Add(elements);
  117. }
  118.  
  119. return ret;
  120. }
  121.  
  122. private static XElement GetEnumerableElement(PropertyInfo info, IEnumerable input)
  123. {
  124. var name = XmlConvert.EncodeName(info.Name);
  125.  
  126. XElement rootElement = new XElement(name);
  127.  
  128. foreach (var ob in input)
  129. {
  130. XElement childElement = ob.GetType().IsSimpleType() ? new XElement(name + "Child", ob) : ob.ToXml();
  131. rootElement.Add(childElement);
  132. }
  133.  
  134. return rootElement;
  135. }
  136. }
  137. }
Add Comment
Please, Sign In to add comment