当前位置: 首页>>代码示例>>C#>>正文


C# XmlSerializerNamespaces类代码示例

本文整理汇总了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;
}
开发者ID:.NET开发者,项目名称:System.Xml.Serialization,代码行数:62,代码来源:XmlSerializerNamespaces


注:本文中的System.Xml.Serialization.XmlSerializerNamespaces类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。