static XSerializer() { defaultNamespaces = new XSerializerNamespaceCollection {PrefixUriPair.Xsi}; defaultParameters = new XSerializerParameters(defaultNamespaces, null); }
/// <summary> /// 将指定的对象序列化,并写入流中。 /// Serialize an object and write it into a Stream. /// </summary> public void Serialize(Stream s, object obj, XSerializerParameters parameters) { if (s == null) throw new ArgumentNullException("s"); if (obj == null) throw new ArgumentNullException("obj"); if (parameters == null) parameters = defaultParameters; GetSerializedDocument(obj, parameters).Save(s, parameters.CompactFormat ? SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces : SaveOptions.None); }
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); }
/// <summary> /// 将指定的对象序列化,并获取序列化后的 XML 文档。 /// Serialize an object into XDocument. /// </summary> public XDocument GetSerializedDocument(object obj, XSerializerParameters parameters) { if (obj == null) throw new ArgumentNullException("obj"); if (parameters == null) parameters = defaultParameters; var root = builder.Serialize(obj, parameters.Context, parameters.Namespaces ?? defaultNamespaces); return new XDocument(new XDeclaration("1.0", "utf-8", "yes"), root); }
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); }