Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. public class MyObject
  2. {
  3. public string Str1 = string.Empty;
  4. public MyEnums.Enum1 E1 = MyEnums.Enum1.Unknown;
  5. public bool Done = false;
  6. };
  7.  
  8. Dictionary<string, MyObject> MyObjectsDic = new Dictionary<string, MyObject>();
  9.  
  10. public static void ToXml(string file, string collectionName, Dictionary<string, object> collection)
  11. {
  12. XElement root = new XElement(collectionName);
  13.  
  14. root.Add(collection.Select(x => new XElement("Item", new XAttribute("Object", x.Key),
  15. x.Value.GetType().GetFields().Select(f => new XElement(f.Name, f.GetValue(x.Value))))));
  16.  
  17. root.Save(file);
  18. }
  19.  
  20. ToXml("MyFile.xml", "MyObjects", MyObjectsDic.ToDictionary(p => p.Key, p => (object)p.Value));
  21.  
  22. List<MyEnums.Enum2> Codes = new List<MyEnums.Enum2>();
  23.  
  24. var a = new MyObject {...};
  25. a.Codes.Add(MyEnums.Enum2.Code1);
  26. a.Codes.Add(MyEnums.Enum2.Code2);
  27. MyObjectsDic.Add("Obj1", a);
  28.  
  29. <Codes>Code1Code2<Codes/>
  30.  
  31. Exception during dumping MyObjectsDic: There was an error reflecting type 'MyObject'.
  32. at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
  33. at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)
  34. at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
  35. at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
  36. at System.Xml.Serialization.XmlSerializer..ctor(Type type)
  37. at MyXmlSerializerHelper.SerializeToXElement[T](T obj, XmlSerializer serializer, Boolean omitStandardNamespaces) in MyXmlSerializerHelper.cs:line 16
  38. at MyXmlSerializerHelper. <SerializeToFile>b__0[T](KeyValuePair'2 x) in MyXmlSerializerHelper.cs:line 5
  39.  
  40. public static class XObjectExtensions
  41. {
  42. public static XElement SerializeToXElement<T>(this IDictionary<string, T> collection, string collectionName)
  43. {
  44. return new XElement(collectionName, collection.Select(x => new XElement("Item", new XAttribute("Object", x.Key), x.Value.SerializeToXElement().Elements())));
  45. }
  46.  
  47. public static XElement SerializeToXElement<T>(this T obj, XmlSerializer serializer = null, bool omitStandardNamespaces = true)
  48. {
  49. var doc = new XDocument();
  50. using (var writer = doc.CreateWriter())
  51. {
  52. XmlSerializerNamespaces ns = null;
  53. if (omitStandardNamespaces)
  54. (ns = new XmlSerializerNamespaces()).Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
  55. (serializer ?? new XmlSerializer(obj.GetType())).Serialize(writer, obj, ns);
  56. }
  57. var element = doc.Root;
  58. if (element != null)
  59. element.Remove();
  60. return element;
  61. }
  62. }
  63.  
  64. <MyObjects>
  65. <Item Object="Obj1">
  66. <Str1>Test object</Str1>
  67. <E1>Unknown</E1>
  68. <Done>false</Done>
  69. <Codes>
  70. <Enum2>Code1</Enum2>
  71. <Enum2>Code2</Enum2>
  72. </Codes>
  73. </Item>
  74. </MyObjects>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement