Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyObject
- {
- public string Str1 = string.Empty;
- public MyEnums.Enum1 E1 = MyEnums.Enum1.Unknown;
- public bool Done = false;
- };
- Dictionary<string, MyObject> MyObjectsDic = new Dictionary<string, MyObject>();
- public static void ToXml(string file, string collectionName, Dictionary<string, object> collection)
- {
- XElement root = new XElement(collectionName);
- root.Add(collection.Select(x => new XElement("Item", new XAttribute("Object", x.Key),
- x.Value.GetType().GetFields().Select(f => new XElement(f.Name, f.GetValue(x.Value))))));
- root.Save(file);
- }
- ToXml("MyFile.xml", "MyObjects", MyObjectsDic.ToDictionary(p => p.Key, p => (object)p.Value));
- List<MyEnums.Enum2> Codes = new List<MyEnums.Enum2>();
- var a = new MyObject {...};
- a.Codes.Add(MyEnums.Enum2.Code1);
- a.Codes.Add(MyEnums.Enum2.Code2);
- MyObjectsDic.Add("Obj1", a);
- <Codes>Code1Code2<Codes/>
- Exception during dumping MyObjectsDic: There was an error reflecting type 'MyObject'.
- at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
- at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)
- at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
- at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
- at System.Xml.Serialization.XmlSerializer..ctor(Type type)
- at MyXmlSerializerHelper.SerializeToXElement[T](T obj, XmlSerializer serializer, Boolean omitStandardNamespaces) in MyXmlSerializerHelper.cs:line 16
- at MyXmlSerializerHelper. <SerializeToFile>b__0[T](KeyValuePair'2 x) in MyXmlSerializerHelper.cs:line 5
- public static class XObjectExtensions
- {
- public static XElement SerializeToXElement<T>(this IDictionary<string, T> collection, string collectionName)
- {
- return new XElement(collectionName, collection.Select(x => new XElement("Item", new XAttribute("Object", x.Key), x.Value.SerializeToXElement().Elements())));
- }
- public static XElement SerializeToXElement<T>(this T obj, XmlSerializer serializer = null, bool omitStandardNamespaces = true)
- {
- var doc = new XDocument();
- using (var writer = doc.CreateWriter())
- {
- XmlSerializerNamespaces ns = null;
- if (omitStandardNamespaces)
- (ns = new XmlSerializerNamespaces()).Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
- (serializer ?? new XmlSerializer(obj.GetType())).Serialize(writer, obj, ns);
- }
- var element = doc.Root;
- if (element != null)
- element.Remove();
- return element;
- }
- }
- <MyObjects>
- <Item Object="Obj1">
- <Str1>Test object</Str1>
- <E1>Unknown</E1>
- <Done>false</Done>
- <Codes>
- <Enum2>Code1</Enum2>
- <Enum2>Code2</Enum2>
- </Codes>
- </Item>
- </MyObjects>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement