System.Runtime.Serialization.XmlFormatterSerializer.Serialize C# (CSharp) Method

Serialize() public method

public Serialize ( Type type, object graph ) : void
type System.Type
graph object
return void
		public void Serialize (Type type, object graph)
		{
			if (graph == null)
				writer.WriteAttributeString ("nil", XmlSchema.InstanceNamespace, "true");
			else {
				QName resolvedQName = null;
				if (resolver != null) {
					XmlDictionaryString rname, rns;
					if (resolver.TryResolveType (graph != null ? graph.GetType () : typeof (object), type, default_resolver, out rname, out rns))
						resolvedQName = new QName (rname.Value, rns.Value);
				}

				Type actualType = graph.GetType ();

				SerializationMap map;
				map = types.FindUserMap (actualType);
				// For some collection types, the actual type does not matter. So get nominal serialization type instead.
				// (The code below also covers the lines above, but I don't remove above lines to avoid extra search cost.)
				if (map == null) {
					actualType = types.GetSerializedType (actualType);
					map = types.FindUserMap (actualType);
				}
				// If it is still unknown, then register it.
				if (map == null) {
					types.Add (actualType);
					map = types.FindUserMap (actualType);
				}

				if (actualType != type && (map == null || map.OutputXsiType)) {
					QName qname = resolvedQName ?? types.GetXmlName (actualType);
					string name = qname.Name;
					string ns = qname.Namespace;
					if (qname == QName.Empty) {
						name = XmlConvert.EncodeLocalName (actualType.Name);
						ns = KnownTypeCollection.DefaultClrNamespaceBase + actualType.Namespace;
					} else if (qname.Namespace == KnownTypeCollection.MSSimpleNamespace)
						ns = XmlSchema.Namespace;
					if (writer.LookupPrefix (ns) == null) // it goes first (extraneous, but it makes att order compatible)
						writer.WriteXmlnsAttribute (null, ns);
					writer.WriteStartAttribute ("i", "type", XmlSchema.InstanceNamespace);
					writer.WriteQualifiedName (name, ns);
					writer.WriteEndAttribute ();
				}
				QName predef = KnownTypeCollection.GetPredefinedTypeName (actualType);
				if (predef != QName.Empty)
					SerializePrimitive (type, graph, predef);
				else
					map.Serialize (graph, this);
			}
		}

Same methods

XmlFormatterSerializer::Serialize ( XmlDictionaryWriter writer, object graph, Type declaredType, KnownTypeCollection types, bool ignoreUnknown, int maxItems, string root_ns, bool preserveObjectReferences, DataContractResolver resolver, DataContractResolver defaultResolver ) : void

Usage Example

コード例 #1
0
 public override void SerializeNonReference(object graph,
                                            XmlFormatterSerializer serializer)
 {
     foreach (object o in (IEnumerable)graph)
     {
         serializer.WriteStartElement(element_qname.Name, XmlName.Namespace, CurrentNamespace);
         serializer.Serialize(element_type, o);
         serializer.WriteEndElement();
     }
 }
All Usage Examples Of System.Runtime.Serialization.XmlFormatterSerializer::Serialize