示例#1
0
        public static void WriteObjectData(string path)
        {
            // Create the object to serialize.
            Person p = new Person("Lynn", "Tsoflias", 9876);

            // Create the writer.
            FileStream          fs     = new FileStream(path, FileMode.Create);
            XmlDictionaryWriter writer =
                XmlDictionaryWriter.CreateTextWriter(fs);

            NetDataContractSerializer ser =
                new NetDataContractSerializer();

            // Use the writer to start a document.
            writer.WriteStartDocument(true);

            // Use the serializer to write the start of the
            // object data. Use it again to write the object
            // data.
            ser.WriteStartObject(writer, p);
            ser.WriteObjectContent(writer, p);

            // Use the serializer to write the end of the
            // object data. Then use the writer to write the end
            // of the document.
            ser.WriteEndObject(writer);
            writer.WriteEndDocument();

            Console.WriteLine("Done");

            // Close and release the writer resources.
            writer.Flush();
            fs.Flush();
            fs.Close();
        }
示例#2
0
        //</snippet6>

        //<snippet7>
        public static void WriteObjectContentInDocument(string path)
        {
            // Create the object to serialize.
            Person p = new Person("Lynn", "Tsoflias", 9876);

            // Create the writer.
            FileStream          fs     = new FileStream(path, FileMode.Create);
            XmlDictionaryWriter writer =
                XmlDictionaryWriter.CreateTextWriter(fs);

            NetDataContractSerializer ser =
                new NetDataContractSerializer();

            // Use the writer to start a document.
            writer.WriteStartDocument(true);
            // Use the writer to write the root element.
            writer.WriteStartElement("Company");
            // Use the writer to write an element.
            writer.WriteElementString("Name", "Microsoft");
            // Use the serializer to write the start, content,
            // and end data.
            ser.WriteStartObject(writer, p);
            writer.WriteStartAttribute("localName");
            writer.WriteString("My Value");
            writer.WriteEndAttribute();
            ser.WriteObjectContent(writer, p);
            ser.WriteEndObject(writer);

            // Use the writer to write the end element
            // and end of the document.
            writer.WriteEndElement();
            writer.WriteEndDocument();

            // Close and release the writer resources.
            writer.Flush();
            fs.Flush();
            fs.Close();
        }