本文整理汇总了C#中System.Xml.Serialization.XmlArrayItemAttribute.XmlArrayItemAttribute构造函数的典型用法代码示例。如果您正苦于以下问题:C# XmlArrayItemAttribute构造函数的具体用法?C# XmlArrayItemAttribute怎么用?C# XmlArrayItemAttribute使用的例子?那么, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在类System.Xml.Serialization.XmlArrayItemAttribute
的用法示例。
在下文中一共展示了XmlArrayItemAttribute构造函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Vehicle
{
public string id;
}
public class Car:Vehicle
{
public string Maker;
}
public class Transportation
{
[XmlArrayItem(),
XmlArrayItem(typeof(Car), ElementName = "Automobile")]
public Vehicle[] MyVehicles;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("XmlArrayItem1.xml");
test.DeserializeObject("XmlArrayItem1.xml");
}
private void SerializeObject(string filename){
// Creates an XmlSerializer for the Transportation class.
XmlSerializer MySerializer = new XmlSerializer(typeof(Transportation));
// Writing the XML file to disk requires a TextWriter.
TextWriter myTextWriter = new StreamWriter(filename);
// Creates the object to serialize.
Transportation myTransportation = new Transportation();
// Creates objects to add to the array.
Vehicle myVehicle= new Vehicle() ;
myVehicle.id = "A12345";
Car myCar = new Car();
myCar.id = "Car 34";
myCar.Maker = "FamousCarMaker";
myTransportation.MyVehicles =
new Vehicle[2] {myVehicle, myCar};
// Serializes the object, and closes the StreamWriter.
MySerializer.Serialize(myTextWriter, myTransportation);
myTextWriter.Close();
}
private void DeserializeObject(string filename)
{
// Creates an XmlSerializer instance.
XmlSerializer mySerializer = new XmlSerializer(typeof(Transportation));
FileStream myFileStream = new FileStream(filename,FileMode.Open);
Transportation myTransportation =
(Transportation) mySerializer.Deserialize(myFileStream);
for(int i = 0; i < myTransportation.MyVehicles.Length;i++)
{
Console.WriteLine(myTransportation.MyVehicles[i].id);
}
}
}
示例2: Main
//引入命名空间
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Vehicle
{
public string id;
}
public class Car:Vehicle
{
public string Maker;
}
public class Transportation
{
[XmlArrayItem(ElementName = "Transportation"),
XmlArrayItem(typeof(Car), ElementName = "Automobile")]
public Vehicle[] MyVehicles;
}
public class Run
{
public static void Main()
{
Run test= new Run();
test.SerializeObject("XmlArrayItem2.xml");
test.DeserializeObject("XmlArrayItem2.xml");
}
private void SerializeObject(string filename)
{
// Creates an XmlSerializer for the Transportation class.
XmlSerializer MySerializer =
new XmlSerializer(typeof(Transportation));
// Writing the XML file to disk requires a TextWriter.
TextWriter myTextWriter = new StreamWriter(filename);
Transportation myTransportation = new Transportation();
Vehicle myVehicle= new Vehicle() ;
myVehicle.id = "A12345";
Car myCar = new Car();
myCar.id = "Car 34";
myCar.Maker = "FamousCarMaker";
Vehicle [] myVehicles = {myVehicle, myCar};
myTransportation.MyVehicles = myVehicles;
// Serializes the object, and closes the StreamWriter.
MySerializer.Serialize(myTextWriter, myTransportation);
myTextWriter.Close();
}
private void DeserializeObject(string filename)
{
// Creates the serializer with the type to deserialize.
XmlSerializer mySerializer =
new XmlSerializer(typeof(Transportation));
FileStream myFileStream = new FileStream(filename,FileMode.Open);
Transportation myTransportation =
(Transportation) mySerializer.Deserialize(myFileStream);
for(int i = 0;i < myTransportation.MyVehicles.Length;i++)
{
Console.WriteLine(myTransportation.MyVehicles[i].id);
}
}
}
示例3: Main
//引入命名空间
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Vehicle
{
public string id;
}
public class Car:Vehicle
{
public string Maker;
}
public class Transportation
{
[XmlArrayItem(typeof(Vehicle)),
XmlArrayItem(typeof(Car))]
public Vehicle[] MyVehicles;
}
public class Run
{
public static void Main()
{
Run test= new Run();
test.SerializeObject("XmlArrayItem3.xml");
test.DeserializeObject("XmlArrayItem3.xml");
}
private void SerializeObject(string filename)
{
// Creates an XmlSerializer.
XmlSerializer MySerializer =
new XmlSerializer(typeof(Transportation));
// Writing the XML file to disk requires a TextWriter.
TextWriter myTextWriter = new StreamWriter(filename);
Transportation myTransportation = new Transportation();
Vehicle myVehicle= new Vehicle() ;
myVehicle.id = "A12345";
Car myCar = new Car();
myCar.id = "Car 34";
myCar.Maker = "FamousCarMaker";
Vehicle [] myVehicles = {myVehicle, myCar};
myTransportation.MyVehicles = myVehicles;
// Serializes the object, and closes the StreamWriter.
MySerializer.Serialize(myTextWriter, myTransportation);
myTextWriter.Close();
}
private void DeserializeObject(string filename)
{
// Creates the serializer with the type to deserialize.
XmlSerializer mySerializer =
new XmlSerializer(typeof(Transportation));
FileStream myFileStream = new FileStream(filename,FileMode.Open);
Transportation myTransportation =
(Transportation) mySerializer.Deserialize(myFileStream);
for(int i = 0;i < myTransportation.MyVehicles.Length;i++)
{
Console.WriteLine(myTransportation.MyVehicles[i].id);
}
}
}
示例4: Main
//引入命名空间
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Vehicle
{
public string id;
}
public class Car:Vehicle
{
public string Maker;
}
public class Transportation
{
[XmlArray]
[XmlArrayItem("Transport", typeof(Vehicle)),
XmlArrayItem("Automobile", typeof(Car))]
public Vehicle[] MyVehicles;
}
public class Run
{
public static void Main()
{
Run test= new Run();
test.SerializeObject("XmlArrayItem4.xml");
test.DeserializeObject("XmlArrayItem4.xml");
}
private void SerializeObject(string filename)
{
// Creates an XmlSerializer for the Transportation class.
XmlSerializer MySerializer =
new XmlSerializer(typeof(Transportation));
// Writing the XML file to disk requires a TextWriter.
TextWriter myTextWriter = new StreamWriter(filename);
Transportation myTransportation = new Transportation();
Vehicle myVehicle= new Vehicle() ;
myVehicle.id = "A12345";
Car myCar = new Car();
myCar.id = "Car 34";
myCar.Maker = "FamousCarMaker";
Vehicle [] myVehicles = {myVehicle, myCar};
myTransportation.MyVehicles = myVehicles;
// Serializes the object, and closes the StreamWriter.
MySerializer.Serialize(myTextWriter, myTransportation);
myTextWriter.Close();
}
private void DeserializeObject(string filename)
{
// Creates an XmlSerializer.
XmlSerializer mySerializer =
new XmlSerializer(typeof(Transportation));
FileStream myFileStream = new FileStream(filename,FileMode.Open);
Transportation myTransportation =
(Transportation) mySerializer.Deserialize(myFileStream);
for(int i = 0;i < myTransportation.MyVehicles.Length;i++)
{
Console.WriteLine(myTransportation.MyVehicles[i].id);
}
}
}