【问题标题】:how to serialize a point3D using XmlSerializer?如何使用 XmlSerializer 序列化 point3D?
【发布时间】:2014-08-11 11:57:38
【问题描述】:
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 的类的其他属性 但是我在序列化时出错。 请让我知道如何实现这一目标或将我指向相关资源。谢谢!

【问题讨论】:

  • I get error while serializing 但是您认为错误消息无关,因此您不发布它不是吗?
  • 是的,错误信息是There was an error generating the XML document
  • 但是有一个内部异常解释细节......

标签: c# wpf serialization


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2019-03-13
    相关资源
    最近更新 更多