本文整理汇总了C#中System.Xml.Serialization.XmlSerializer.Serialize方法的典型用法代码示例。如果您正苦于以下问题:C# XmlSerializer.Serialize方法的具体用法?C# XmlSerializer.Serialize怎么用?C# XmlSerializer.Serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Serialization.XmlSerializer
的用法示例。
在下文中一共展示了XmlSerializer.Serialize方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Calculate
//引入命名空间
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class OrderedItem
{
public string ItemName;
public string Description;
public decimal UnitPrice;
public int Quantity;
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
public class Test{
public static void Main()
{
Test t = new Test();
// Write a purchase order.
t.SerializeObject("simple.xml");
}
private void SerializeObject(string filename)
{
Console.WriteLine("Writing With XmlTextWriter");
XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem));
OrderedItem i = new OrderedItem();
i.ItemName = "Widget";
i.Description = "Regular Widget";
i.Quantity = 10;
i.UnitPrice = (decimal) 2.30;
i.Calculate();
// Create an XmlTextWriter using a FileStream.
Stream fs = new FileStream(filename, FileMode.Create);
XmlWriter writer =
new XmlTextWriter(fs, Encoding.Unicode);
// Serialize using the XmlTextWriter.
serializer.Serialize(writer, i);
writer.Close();
}
}
示例2: Calculate
//引入命名空间
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class OrderedItem
{
[XmlElement(Namespace = "http://www.cpandl.com")]
public string ItemName;
[XmlElement(Namespace = "http://www.cpandl.com")]
public string Description;
[XmlElement(Namespace="http://www.cohowinery.com")]
public decimal UnitPrice;
[XmlElement(Namespace = "http://www.cpandl.com")]
public int Quantity;
[XmlElement(Namespace="http://www.cohowinery.com")]
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
public class Test{
public static void Main()
{
Test t = new Test();
// Write a purchase order.
t.SerializeObject("simple.xml");
}
private void SerializeObject(string filename)
{
Console.WriteLine("Writing With XmlTextWriter");
XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem));
OrderedItem i = new OrderedItem();
i.ItemName = "Widget";
i.Description = "Regular Widget";
i.Quantity = 10;
i.UnitPrice = (decimal) 2.30;
i.Calculate();
// Create an XmlSerializerNamespaces object.
XmlSerializerNamespaces ns =
new XmlSerializerNamespaces();
// Add two namespaces with prefixes.
ns.Add("inventory", "http://www.cpandl.com");
ns.Add("money", "http://www.cohowinery.com");
// Create an XmlTextWriter using a FileStream.
Stream fs = new FileStream(filename, FileMode.Create);
XmlWriter writer =
new XmlTextWriter(fs, new UTF8Encoding());
// Serialize using the XmlTextWriter.
serializer.Serialize(writer, i, ns);
writer.Close();
}
}
示例3: Calculate
//引入命名空间
using System;
using System.IO;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class OrderedItem
{
[XmlElement(Namespace = "http://www.cpandl.com")]
public string ItemName;
[XmlElement(Namespace = "http://www.cpandl.com")]
public string Description;
[XmlElement(Namespace="http://www.cohowinery.com")]
public decimal UnitPrice;
[XmlElement(Namespace = "http://www.cpandl.com")]
public int Quantity;
[XmlElement(Namespace="http://www.cohowinery.com")]
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
public class Test{
public static void Main(string[] args)
{
Test t = new Test();
// Write a purchase order.
t.SerializeObject("simple.xml");
}
private void SerializeObject(string filename)
{
Console.WriteLine("Writing With TextWriter");
// Create an XmlSerializer instance using the type.
XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem));
OrderedItem i = new OrderedItem();
i.ItemName = "Widget";
i.Description = "Regular Widget";
i.Quantity = 10;
i.UnitPrice = (decimal) 2.30;
i.Calculate();
// Create an XmlSerializerNamespaces object.
XmlSerializerNamespaces ns =
new XmlSerializerNamespaces();
// Add two namespaces with prefixes.
ns.Add("inventory", "http://www.cpandl.com");
ns.Add("money", "http://www.cohowinery.com");
// Create a StreamWriter to write with.
TextWriter writer = new StreamWriter(filename);
/* Serialize using the object using the TextWriter
and namespaces. */
serializer.Serialize(writer, i, ns);
writer.Close();
}
}
示例4: Calculate
//引入命名空间
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class OrderedItem
{
public string ItemName;
public string Description;
public decimal UnitPrice;
public int Quantity;
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
public class Test{
public static void Main(string[] args)
{
Test t = new Test();
// Write a purchase order.
t.SerializeObject("simple.xml");
}
private void SerializeObject(string filename)
{
Console.WriteLine("Writing With TextWriter");
XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem));
OrderedItem i = new OrderedItem();
i.ItemName = "Widget";
i.Description = "Regular Widget";
i.Quantity = 10;
i.UnitPrice = (decimal) 2.30;
i.Calculate();
/* Create a StreamWriter to write with. First create a FileStream
object, and create the StreamWriter specifying an Encoding to use. */
FileStream fs = new FileStream(filename, FileMode.Create);
TextWriter writer = new StreamWriter(fs, new UTF8Encoding());
// Serialize using the XmlTextWriter.
serializer.Serialize(writer, i);
writer.Close();
}
}
示例5: Calculate
//引入命名空间
using System;
using System.IO;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class OrderedItem
{
public string ItemName;
public string Description;
public decimal UnitPrice;
public int Quantity;
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
public class Test{
public static void Main(string[] args)
{
Test t = new Test();
// Write a purchase order.
t.SerializeObject("simple.xml");
}
private void SerializeObject(string filename)
{
Console.WriteLine("Writing With Stream");
XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem));
OrderedItem i = new OrderedItem();
i.ItemName = "Widget";
i.Description = "Regular Widget";
i.Quantity = 10;
i.UnitPrice = (decimal) 2.30;
i.Calculate();
// Create a FileStream to write with.
Stream writer = new FileStream(filename, FileMode.Create);
// Serialize the object, and close the TextWriter
serializer.Serialize(writer, i);
writer.Close();
}
}
示例6: Calculate
//引入命名空间
using System;
using System.IO;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class OrderedItem {
[XmlElement(Namespace = "http://www.cpandl.com")]
public string ItemName;
[XmlElement(Namespace = "http://www.cpandl.com")]
public string Description;
[XmlElement(Namespace="http://www.cohowinery.com")]
public decimal UnitPrice;
[XmlElement(Namespace = "http://www.cpandl.com")]
public int Quantity;
[XmlElement(Namespace="http://www.cohowinery.com")]
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate() {
LineTotal = UnitPrice * Quantity;
}
}
public class Test {
public static void Main() {
Test t = new Test();
// Write a purchase order.
t.SerializeObject("simple.xml");
t.DeserializeObject("simple.xml");
}
private void SerializeObject(string filename) {
Console.WriteLine("Writing With Stream");
XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem));
OrderedItem i = new OrderedItem();
i.ItemName = "Widget";
i.Description = "Regular Widget";
i.Quantity = 10;
i.UnitPrice = (decimal) 2.30;
i.Calculate();
// Create an XmlSerializerNamespaces object.
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
// Add two prefix-namespace pairs.
ns.Add("inventory", "http://www.cpandl.com");
ns.Add("money", "http://www.cohowinery.com");
// Create a FileStream to write with.
Stream writer = new FileStream(filename, FileMode.Create);
// Serialize the object, and close the TextWriter
serializer.Serialize(writer, i, ns);
writer.Close();
}
private void DeserializeObject(string filename) {
Console.WriteLine("Reading with Stream");
// Create an instance of the XmlSerializer.
XmlSerializer serializer = new XmlSerializer(typeof(OrderedItem));
// Writing the file requires a Stream.
Stream reader= new FileStream(filename,FileMode.Open);
// Declare an object variable of the type to be deserialized.
OrderedItem i;
/* Use the Deserialize method to restore the object's state
using data from the XML document. */
i = (OrderedItem) serializer.Deserialize(reader);
// Write out the properties of the object.
Console.Write(
i.ItemName + "\t" +
i.Description + "\t" +
i.UnitPrice + "\t" +
i.Quantity + "\t" +
i.LineTotal);
}
}
示例7: Main
//引入命名空间
using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
class MainClass{
public static void Main(){
try {
ShapedRoot sr = new ShapedRoot();
sr.node = new Shaped();
sr.node.s1 = "a value";
sr.node.s2 = "another value";
sr.node.s3 = "uuid:1AE0964A-2B30-4a02-96F2-325B92B4E92C";
StringWriter sw = new StringWriter();
XmlTextWriter tw = new XmlTextWriter( sw );
tw.Formatting = Formatting.Indented;
tw.Indentation = 4;
XmlSerializer ser = new XmlSerializer( typeof( ShapedRoot ) );
ser.Serialize( tw, sr );
tw.Close();
sw.Close();
}
catch( Exception exc )
{
Console.WriteLine( exc.Message );
}
}
}
[XmlRoot( ElementName="sroot", Namespace="urn:my-examples:shaping" )]
public class ShapedRoot
{
public ShapedRoot()
{
}
public Shaped node;
}
[XmlType( Namespace="urn:my-examples:shaping" )]
public class Shaped
{
public Shaped()
{
}
[XmlAttribute]
public string s1;
[XmlElement( ElementName = "string2" )]
public string s2;
[XmlElement( DataType = "anyURI" )]
public string s3;
}