public XElement SerializeRoot(object obj, Type rootType, XName name, XSerializerNamespaceCollection namespaces, SerializationScope globalScope) { Debug.Assert(rootType.IsInstanceOfType(obj)); Debug.Assert(namespaces != null); var root = SerializeXElement(obj, rootType, name, globalScope); //导入命名空间。 foreach (var ns in namespaces) root.SetAttributeValue(XNamespace.Xmlns + ns.Prefix, ns.Uri); //处理导入的类型。 var nsCounter = 0; foreach (var descendant in root.Descendants()) { var actualTypeName = descendant.Annotation<XName>(); if (actualTypeName != null) { if (actualTypeName.Namespace == descendant.GetDefaultNamespace()) { descendant.SetAttributeValue(SerializationHelper.Xsi + "type", actualTypeName.LocalName); } else { var prefix = descendant.GetPrefixOfNamespace(actualTypeName.Namespace); if (prefix == null) { nsCounter++; prefix = "nss" + nsCounter; descendant.SetAttributeValue(XNamespace.Xmlns + prefix, actualTypeName.NamespaceName); } descendant.SetAttributeValue(SerializationHelper.Xsi + "type", prefix + ":" + actualTypeName.LocalName); } descendant.RemoveAnnotations<XName>(); } } return root; }
static XSerializer() { defaultNamespaces = new XSerializerNamespaceCollection {PrefixUriPair.Xsi}; defaultParameters = new XSerializerParameters(defaultNamespaces, null); }
public XSerializerParameters(XSerializerNamespaceCollection namespaces, object context) { Namespaces = namespaces; Context = context; }
public XSerializerParameters(XSerializerNamespaceCollection namespaces) : this(namespaces, null) { }
/// <summary> /// 将指定的对象序列化,并写入流中。 /// Serialize an object and write it into a Stream. /// </summary> public void Serialize(Stream s, object obj, XSerializerNamespaceCollection namespaces) { Serialize(s, obj, new XSerializerParameters(namespaces)); }
public void SerializationTest() { var s = new XSerializer(typeof(MyObject1), new[] { typeof(MyObject2) }); var ns = new XSerializerNamespaceCollection { {"n1", MyObject1.MyUri1}, {"n2", MyObject1.MyUri2}, PrefixUriPair.Xsi }; var p = new XSerializerParameters(ns); var obj = new MyObject1 { Property1 = 123e45d, Array1 = new object[] { "abc", 123.4567, "def", new MyObject1(), new MyObject2() }, AnotherObject = new MyObject1(), myObject = new MyObject2() }; //There are intentional spaces left in the strings. obj.List1.Add("越过长城,走向世界。 "); obj.List1.Add("\t\tAcross the Great Wall we can reach every corner in the world."); var doc = s.GetSerializedDocument(obj, p); Trace.WriteLine(doc); var obj1 = (MyObject1)s.Deserialize(doc, null); //Debug.Print("Deserialize Obj : {0}", obj1.GetHashCode()); Assert.AreEqual(obj.Property1, obj1.Property1); Assert.AreEqual(obj.Property1, obj1.Property1); Assert.AreEqual(obj.List1.Count, obj1.List1.Count); Assert.AreEqual(obj.List1[0], obj1.List1[0]); Assert.AreEqual(obj.List1[1], obj1.List1[1]); Assert.AreEqual(obj.Array1.Length, obj1.Array1.Length); }
public void SerializationProfiling() { const int repetitions = 1000; var s = new XSerializer(typeof(MyObject1)); var ns = new XSerializerNamespaceCollection { {"n1", MyObject1.MyUri1}, {"n2", MyObject1.MyUri2}, PrefixUriPair.Xsi }; var p = new XSerializerParameters(ns); var obj = new MyObject1 { Property1 = 123e45d, Array1 = new object[] { "abc", 123.4567, "def", new MyObject1() }, AnotherObject = new MyObject1() }; //There are intentional spaces left in the strings. obj.List1.Add("越过长城,走向世界。 "); obj.List1.Add("\t\tAcross the Great Wall we can reach every corner in the world."); var doc = s.GetSerializedDocument(obj, p); Trace.WriteLine(doc); var obj1 = (MyObject1)s.Deserialize(doc, null); // Profiling for (var i = 0; i < obj.Array1.Length; i++) { if (obj.Array1[i].GetType().IsValueType) Assert.AreEqual(obj.Array1[i], obj1.Array1[i]); } var sw = Stopwatch.StartNew(); for (int i = 0; i < repetitions; i++) { // ReSharper disable once ReturnValueOfPureMethodIsNotUsed s.GetSerializedDocument(obj, p).ToString(); } Trace.Write("Serialization elapsed ms : "); Trace.WriteLine(sw.Elapsed.TotalMilliseconds / repetitions); sw.Restart(); for (int i = 0; i < repetitions; i++) { s.Deserialize(doc, null); } Trace.Write("Deserialization elapsed ms : "); Trace.WriteLine(sw.Elapsed.TotalMilliseconds / repetitions); }