XMLSerializer 未序列化 DateTime

2023-12-11

输入...注意注释是由 xsd 工具生成的代码。它位于一个 31,834 行的文件中,并且是专有的,但我在这里放了一个粗略的近似值。

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class comment
{
   private System.DateTime commentDateField;
   private bool commentDateFieldSpecified;
   [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
   public System.DateTime commentDate
   { 
      get 
      {
         return this.commentDateField;
      }
      set
      {
         this.commentDateField = value;
      }  
   }
   [System.Xml.Serialization.XmlIgnoreAttribute()]
   public bool commentDateSpecified
   { 
      get 
      {
         return this.commentDateFieldSpecified;
      }
      set
      {
         this.commentDateFieldSpecified = value;
      }  
   }
   //other fields omitted for clarity
}
comment c = new comment();
c.text = txtComment.Text;
c.commentDate = DateTime.Now;
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlSerializer xs = new XmlSerializer(typeof(comment));
xs.serialize(sw as TextWriter, c);
string output = sb.ToString();

输出->

<comment>
  <text>My Comment Text</text>
</comment>

日期在哪里?


您需要设置才能包含在 XML 中

 c.commentDateSpecified = true;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

XMLSerializer 未序列化 DateTime 的相关文章

随机推荐