本文整理汇总了C#中System.Xml.Serialization.XmlSerializerNamespaces类的典型用法代码示例。如果您正苦于以下问题:C# XmlSerializerNamespaces类的具体用法?C# XmlSerializerNamespaces怎么用?C# XmlSerializerNamespaces使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
XmlSerializerNamespaces类属于System.Xml.Serialization命名空间,在下文中一共展示了XmlSerializerNamespaces类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("XmlNamespaces.xml");
}
public void SerializeObject(string filename)
{
XmlSerializer s = new XmlSerializer(typeof(Books));
// Writing a file requires a TextWriter.
TextWriter t = new StreamWriter(filename);
/* Create an XmlSerializerNamespaces object and add two
prefix-namespace pairs. */
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("books", "http://www.cpandl.com");
ns.Add("money", "http://www.cohowinery.com");
// Create a Book instance.
Book b = new Book();
b.TITLE = "A Book Title";
Price p = new Price();
p.price = (decimal) 9.95;
p.currency = "US Dollar";
b.PRICE = p;
Books bks = new Books();
bks.Book = b;
s.Serialize(t,bks,ns);
t.Close();
}
}
public class Books
{
[XmlElement(Namespace = "http://www.cohowinery.com")]
public Book Book;
}
[XmlType(Namespace ="http://www.cpandl.com")]
public class Book
{
[XmlElement(Namespace = "http://www.cpandl.com")]
public string TITLE;
[XmlElement(Namespace ="http://www.cohowinery.com")]
public Price PRICE;
}
public class Price
{
[XmlAttribute(Namespace = "http://www.cpandl.com")]
public string currency;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal price;
}