views:

316

answers:

2

Input...note that comment is generated code from the xsd tool. It's in a 31,834 line file and proprietary, but I put a rough approximation in here.

[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();

output->

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

Where's the date?

+3  A: 

The following works, you need to show the definition of comment:

public class comment
{
    public string text { get; set; }
    public DateTime commentDate { get; set; }
}

XmlSerializer serializer = new XmlSerializer(typeof(comment));
comment comment = new comment { text = "test", commentDate = DateTime.Now };
using (MemoryStream stream = new MemoryStream())
using (StreamReader reader = new StreamReader(stream))
{
    serializer.Serialize(stream, comment);
    stream.Position = 0;
    Console.WriteLine(reader.ReadToEnd());
}
Yuriy Faktorovich
you're missing some crucial `using` statements as well...
Stan R.
Don't know about *crucial* for testing. I have bitched about other people not having using statements on here as well, so I take your point.
Yuriy Faktorovich
I guess you're right they are not crucial for testing. Just saying that other people that come across this code to use as their own, might just copy and paste the code blindly.
Stan R.
Modified for the copy paste crowd.
Yuriy Faktorovich
I've thought a lot about inclusion of `using` statements as well, then I figure if paster can't decipher correct usings in C# then likely they also won't find the C# compiler or know how to set breakpoints to analyze snippets, and they'll also wonder why pasted code won't compile (missing a class), etc. etc... A certain level of basic competency is required especially if we're already in the XML namespace here and dealing with serialization.
John K
Not mention if you're copying and pasting code, and you don't understand it or test it, you deserve whatever memory leaks you get.
Matt Jordan
+1  A: 

Pavel's comment was the correct answer.

Also, you say "omitted other fields for clarity". Is there, by chance, a field or property named commentDateSpecified among those fields?

MattMcKnight