Set XML tag name for Serialization
来源:互联网 发布:hp1020打印机端口 编辑:程序博客网 时间:2024/04/29 05:36
using System;
using System.IO;
using System.Xml.Serialization;
public class Serializer {
public static void Main(string [] args) {
Personnel personnel = CreatePersonnel();
XmlSerializer serializer = new XmlSerializer(typeof(Personnel));
using (FileStream stream = File.OpenWrite("Employees.xml")) {
serializer.Serialize(stream, personnel);
}
}
private static Personnel CreatePersonnel() {
Personnel personnel = new Personnel();
personnel.Employees = new Employee [] {new Employee()};
personnel.Employees[0].FirstName = "Joe";
personnel.Employees[0].MiddleInitial = "M";
personnel.Employees[0].LastName = "Lee";
personnel.Employees[0].Addresses = new Address [] {new Address()};
personnel.Employees[0].Addresses[0].AddressType = AddressType.Home;
personnel.Employees[0].Addresses[0].Street = new string [] {"999 Colluden"};
personnel.Employees[0].Addresses[0].City = "Vancouver";
personnel.Employees[0].Addresses[0].State = State.BC;
personnel.Employees[0].Addresses[0].Zip = "V5V 4X7";
personnel.Employees[0].HireDate = new DateTime(2001,1,1);
return personnel;
}
}
[Serializable]
public enum AddressType {
Home,
Office
}
[Serializable]
public enum State {
[XmlEnum(Name="British C")]BC,
[XmlEnum(Name="Sask")]SK
}
[Serializable]
public class Address {
[XmlAttribute(AttributeName="type")] public AddressType AddressType;
[XmlElement(ElementName="street")] public string[] Street;
[XmlElement(ElementName="city")] public string City;
[XmlElement(ElementName="state")] public State State;
[XmlElement(ElementName="zip")] public string Zip;
}
[Serializable]
public class TelephoneNumber {
[XmlAttribute(AttributeName="type")] public AddressType AddressType;
[XmlElement(ElementName="areacode")] public string AreaCode;
[XmlElement(ElementName="exchange")] public string Exchange;
[XmlElement(ElementName="number")] public string Number;
}
[Serializable]
public class Employee {
[XmlAttribute(AttributeName="firstname")] public string FirstName;
[XmlAttribute(AttributeName="middleinitial")] public string MiddleInitial;
[XmlAttribute(AttributeName="lastname")] public string LastName;
[XmlArray(ElementName="addresses")]
[XmlArrayItem(ElementName="address")] public Address [] Addresses;
[XmlArray(ElementName="telephones")]
[XmlArrayItem(ElementName="telephone")] public TelephoneNumber [] TelephoneNumbers;
[XmlAttribute(AttributeName="hiredate")] public DateTime HireDate;
}
[Serializable]
[XmlRoot(ElementName="personnel")]
public class Personnel {
[XmlElement(ElementName="employee")]
public Employee [] Employees;
}
- Set XML tag name for Serialization
- Attribute name invalid for tag property according to TLD 异常
- cvs server: sticky tag 'tag.num' for file 'file.name' is not a branch(转)
- XML Serialization Using C#...
- XML Serialization Introduction
- XML Serialization Using C#
- Xml.Serialization.XmlElement
- XML Serialization and Deserialization
- 【Serialization】Xml Serialize Helper
- Attribute name invalid for tag form according to TLD异常解决办法
- ReducedHTMLParser:558 Invalid tag found: unexpected input while looking for attr name or '/>' 错误
- STRUTS开发问题?Attribute name invalid for tag form according to TLD
- Attribute name invalid for tag property according to TLD异常错误
- struts2文件下载错误(<param name="inputName"> tag specified for this action.”)(实验楼)
- 在applicationContext.xml中配置Service层时 name Tag ‘property’ must have a ‘name’ attribute 出错的问题
- ERROR Error: If ngModel is used within a form tag, either the name attribute must be set or the form
- How should I set up tag files for a multi-level directory hierarchy?
- XML Serialization of Java Objects
- 关闭myeclipse的Quick Update自动更新功能
- 随笔
- 网页设计很糟糕的10个原因
- VC++ 搭建驱动开发环境
- 英特尔何淑芬:MeeGo是不同平台的桥梁
- Set XML tag name for Serialization
- 数组初始化
- Tromino谜题的解法
- sybase
- Ext.Ajax.request和formPanel.getForm().submit两种提交方法的异同
- 英特尔力挺MeeGo系统 展讯前副总周承云加盟
- 基于LPC2210的RTL8019AS以太网驱动系统设计(四)
- myeclipse8.5 workspace配置丢失问题
- [Google Android] 理解NDK(1)-- 编译Android .so文件