Autodesk Vault API 类的Xml Serialization攻略

译自Vault API帮助文档中一篇 Doug Redmond写的knowledgebase article. Doug's 博客是 It's All Just Ones and Zeros

你也许不知道所有Vault API 类都可以 XML 序列化 - 这意味着很容易把API对象转换为 XML 文本,反之亦然。 

很多种情况下可以使用此功能,例如:

  • 它可以帮助您调试代码。如果您具有大量数据的对象,将该对象存为XML格式方便研究。
  • 如果您向ADN提交问题,可以将XML序列化的对象作为您的问题说明的一部分。
  • 存储信息供以后使用。只需记住,此信息在下一版本中可能会更改。
  • 您可以使用XML序列化后的信息作为检测对象是否已发生变更的参照物。例如您编写了一个设置生命周期的程序,您希望您的程序检测到是否有人干预了生命周期设置。

下面是一些Vault API XML 序列化的示例代码:

C#:

private bool DidItChange(LfCycDef def)

    // serialize the object 
    StringBuilder currentXML = new StringBuilder(); 
    XmlSerializer serializer = new XmlSerializer(typeof(DocEx.LfCycDef)); 

    using (TextWriter writer = new StringWriter(currentXML)) 
    { 
        serializer.Serialize(writer, def); 
    } 

    // read in the stored XML 
    string originalXML = System.IO.File.ReadAllText("correct.xml");
 

    // now all you have to do is compare the two 
    return (originalXML != currentXML.ToString()); }

VB:

private function  DidItChange(def as LfCycDef ) as boolean
    ' serialize the object
    dim currentXML as StringBuilder  = new StringBuilder()
    dim serializer as XmlSerializer  = new XmlSerializer(GetType(LfCycDef))
    using writer as TextWriter = new System.IO.StringWriter(currentXML)
        serializer.Serialize(writer, def)
    end using

      ' read in the stored XML
    dim originalXML as string  = System.IO.File.ReadAllText("flex.xml")  

    ' now all you have to do is compare the two
    return (originalXML <> currentXML.ToString())

end function

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值