如何使用XmlSerializer序列化point3D

本文关键字:point3D 序列化 XmlSerializer 何使用 | 更新日期: 2023-09-27 18:17:22

public class Placement 
{
   public Point3D Location { get; set; }
   public Point3D Axis { get; set; }
   public Point3D Direction { get; set; }
}

public class Attribute1
{
  public string Key { get; set; }
  public Type Type { get; set; }
  public Object Value { get; set; }
}
class Program
{
  static void Main(string[] args)
  {
     Attribute1 a =  new Attribute1();
     a.Key = "test";
     var p = new Placement();
     p.Axis = new Point3D(12.0, 22.09, 0);
     p.Location = new Point3D(12.0, 22.09, 0);
     p.Direction = new Point3D(12.0, 22.09, 0);
     a.Value = p;
     var serializer = new XmlSerializer(typeof(Attribute1));
     var path = "E:''details.xml";
     using (TextWriter writer = new StreamWriter(path))
     {
        serializer.Serialize(writer, a);
     }
     Console.Read();
   }    
}

我试图用XmlSerializer序列化Point3D。我使用XmlSerializer来序列化包含Attribute1的类的其他属性但是我在序列化的时候出错了。请让我知道如何实现这一目标或指出我的相关资源。谢谢!

如何使用XmlSerializer序列化point3D

您已经告诉它您想要序列化Attribute1而不是Placement。只修改这一行:

var serializer = new XmlSerializer(typeof(Attribute1), new Type[] { typeof(Placement) });