📄 customer.cs
字号:
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
[XmlSchemaProvider("CreateCustomerSchema")]
public class Customer : IXmlSerializable
{
private string _firstName;
private string _lastName;
private string _address;
public Customer()
{
}
public Customer(string firstName, string lastName, string address)
{
_firstName = firstName;
_lastName = lastName;
_address = address;
}
public static XmlQualifiedName CreateCustomerSchema(XmlSchemaSet set)
{
XmlSchema schema = new XmlSchema();
schema.Id = "Test";
schema.TargetNamespace = "urn:types-wrox-com";
XmlSchemaComplexType type = new XmlSchemaComplexType();
type.Name = "customerType";
XmlSchemaAttribute firstNameAttr = new XmlSchemaAttribute();
firstNameAttr.Name = "firstName";
type.Attributes.Add(firstNameAttr);
XmlSchemaAttribute lastNameAttr = new XmlSchemaAttribute();
lastNameAttr.Name = "lastName";
type.Attributes.Add(lastNameAttr);
XmlSchemaAttribute addressAttr = new XmlSchemaAttribute();
addressAttr.Name = "address";
type.Attributes.Add(addressAttr);
XmlSchemaElement customerElement = new XmlSchemaElement();
customerElement.Name = "customer";
XmlQualifiedName name = new XmlQualifiedName("customerType", "urn:types-wrox-com");
customerElement.SchemaTypeName = name;
schema.Items.Add(type);
schema.Items.Add(customerElement);
set.Add(schema);
return name;
}
public XmlSchema GetSchema()
{
return (null);
}
public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement("customer", "urn:wrox-com");
writer.WriteAttributeString("firstName", _firstName);
writer.WriteAttributeString("lastName", _lastName);
writer.WriteAttributeString("address", _address);
writer.WriteEndElement();
}
public void ReadXml(XmlReader reader)
{
XmlNodeType type = reader.MoveToContent();
if ((type == XmlNodeType.Element) && (reader.LocalName == "customer"))
{
_firstName = reader["firstName"];
_lastName = reader["lastName"];
_address = reader["address"];
}
}
public override string ToString()
{
return (string.Format("Person [{0} {1}]"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -