111,080
社区成员




<?xml version="1.0"?>
<Root>
<Test name="a" xsi:type="Day" />
<Test name="b" xsi:type="Month" />
<Test name="c" xsi:type="Year" />
</Root>
[XmlInclude(typeof(Manager))]
[XmlType(Namespace="http://www.w3.org/2001/XMLSchema-instance")] //这一句
[Serializable]
public class Employee
{
[XmlAttribute]
public string Name;
}
class Program
{
static void Main()
{
List<Test> tests = new List<Test>();
tests.Add( new Test( "a", "Day" ) );
tests.Add( new Test( "b", "Month" ) );
tests.Add( new Test( "c", "Year" ) );
string filename = @"C:\temp.txt";
XmlSerializer xs = new XmlSerializer( typeof( List<Test> ) );
using ( FileStream fs = new FileStream( filename, FileMode.Create ) )
{
xs.Serialize( fs, tests );
}
using ( FileStream fs = new FileStream( filename, FileMode.Open ) )
{
tests = xs.Deserialize( fs ) as List<Test>;
foreach ( Test test in tests )
{
Console.WriteLine( test.Name + " " + test.Type );
}
}
Console.ReadLine();
}
}
public class Test
{
[XmlAttribute( AttributeName = "name" )]
public string Name { get; set; }
[XmlAttribute( AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance" )] //要正确设置 Namespace
public string Type { get; set; }
public Test() { }
public Test( string name, string type )
{
Name = name;
Type = type;
}
}
using System;
using System.IO;
using System.Xml.Serialization;
[Serializable]
[XmlRoot( Namespace = "http://www.csdn.net" )]
public class Group
{
public List<Employee> Employees = new List<Employee>();
}
[XmlInclude( typeof( Manager ) )]
[Serializable]
public class Employee
{
[XmlAttribute]
public string Name;
}
[Serializable]
public class Manager : Employee
{
[XmlAttribute]
public int Level;
}
public class Run
{
public static void Main()
{
Run test = new Run();
string path = @"D:\Temp\xsitype.txt";
test.SerializeObject( path );
test.DeserializeObject( path );
}
public void SerializeObject( string filename )
{
using ( FileStream fs = new FileStream( filename, FileMode.Create ) )
{
XmlSerializer s = new XmlSerializer( typeof( Group ) );
TextWriter writer = new StreamWriter( fs );
Group group = new Group();
Manager manager = new Manager();
Employee emp1 = new Employee();
Employee emp2 = new Employee();
manager.Name = "Zeus";
manager.Level = 2;
emp1.Name = "Ares";
emp2.Name = "Artemis";
group.Employees.Add( manager );
s.Serialize( writer, group);
}
}
public void DeserializeObject( string filename )
{
FileStream fs = new FileStream( filename, FileMode.Open );
XmlSerializer x = new XmlSerializer( typeof( Group ) );
Group g = ( Group ) x.Deserialize( fs );
Console.Write( "Members:" );
foreach ( Employee e in g.Employees )
{
Console.WriteLine( "\t" + e.Name );
}
Console.ReadLine();
}
}
class Program
{
static void Main()
{
List<Test> tests = new List<Test>();
tests.Add(new Test("a", "Day"));
tests.Add(new Test("b", "Month"));
tests.Add(new Test("c", "Year"));
XmlSerializer xs = new XmlSerializer(typeof(List<Test>));
xs.Serialize(Console.Out, tests);
}
}
public class Test
{
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="Type", Namespace="http://www.w3.org/2001/XMLSchema-instance")] //要正确设置 Namespace
public string Type { get; set; }
public Test() { }
public Test(string name, string type)
{
Name = name;
Type = type;
}
}