本文整理汇总了C#中System.Xml.Serialization.XmlTypeAttribute.XmlTypeAttribute构造函数的典型用法代码示例。如果您正苦于以下问题:C# XmlTypeAttribute构造函数的具体用法?C# XmlTypeAttribute怎么用?C# XmlTypeAttribute使用的例子?那么, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在类System.Xml.Serialization.XmlTypeAttribute
的用法示例。
在下文中一共展示了XmlTypeAttribute构造函数的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
using System.Xml.Serialization;
public class Person
{
public string personName;
public Address address;
}
public class Address
{
public string state;
public string zip;
}
public class PersonTypeAttribute
{
public static void Main()
{
PersonTypeAttribute myPersonTypeAttribute= new PersonTypeAttribute();
myPersonTypeAttribute.SerializeObject("XmlType.xml");
}
public XmlSerializer CreateOverrider()
{
XmlAttributeOverrides personOverride = new XmlAttributeOverrides();
XmlAttributes personAttributes = new XmlAttributes();
XmlTypeAttribute personType = new XmlTypeAttribute();
personType.TypeName = "Employee";
personType.Namespace = "http://www.microsoft.com";
personAttributes.XmlType = personType;
XmlAttributes addressAttributes = new XmlAttributes();
// Create 'XmlTypeAttribute' with 'TypeName' as an argument.
XmlTypeAttribute addressType = new XmlTypeAttribute("Address");
addressType.Namespace = "http://www.microsoft.com";
addressAttributes.XmlType=addressType;
personOverride.Add(typeof(Person) ,personAttributes);
personOverride.Add(typeof(Address),addressAttributes);
XmlSerializer myXmlSerializer = new XmlSerializer
(typeof(Person), personOverride);
return myXmlSerializer;
}
public void SerializeObject(string filename)
{
XmlSerializer myXmlSerializer = CreateOverrider();
Address myAddress = new Address();
myAddress.state="AAA";
myAddress.zip="11111";
Person myPerson = new Person();
myPerson.personName="Smith";
myPerson.address=myAddress;
// Serialize to a file.
TextWriter writer = new StreamWriter(filename);
myXmlSerializer.Serialize(writer, myPerson);
}
}