Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a lot of code, but it does what you want. The gist is that you can create a new class (<code>LeadingZero</code> in this example) and implement <code>IXmlSerializable</code> to control how you read/write from the XML stream. Hope this helps:</p> <pre><code> using System; using System.IO; using System.Xml.Serialization; namespace StackOverflow { [Serializable] public class LeadingZero : IXmlSerializable { public int Value { get; set; } public LeadingZero() { Value = 0; } public LeadingZero(int value) { this.Value = value; } public override string ToString() { return Value.ToString("00"); } #region IXmlSerializable Members public System.Xml.Schema.XmlSchema GetSchema() { return null; } public void ReadXml(System.Xml.XmlReader reader) { string s = reader.ReadElementString(); int i; if (int.TryParse(s, out i)) { Value = i; } } public void WriteXml(System.Xml.XmlWriter writer) { writer.WriteString(Value.ToString("00")); } #endregion } [Serializable] public class Complex { public LeadingZero ValidFromMonth { get; set; } public LeadingZero ValidFromYear { get; set; } public LeadingZero ExpiresEndMonth { get; set; } public LeadingZero ExpiresEndYear { get; set; } } class Program { static void Main() { var seven = new LeadingZero(7); XmlSerializer xml = new XmlSerializer(typeof(LeadingZero)); StringWriter writer; writer = new StringWriter(); xml.Serialize(writer, seven); string s = writer.ToString(); Console.WriteLine(seven); Console.WriteLine(); Console.WriteLine(s); Console.WriteLine(); var newSeven = xml.Deserialize(new StringReader(s)) as LeadingZero; Console.WriteLine(newSeven ?? new LeadingZero(0)); var complicated = new Complex() { ValidFromMonth = new LeadingZero(7), ValidFromYear = new LeadingZero(2009), ExpiresEndMonth = new LeadingZero(6), ExpiresEndYear = new LeadingZero(2010) }; Console.WriteLine(); writer = new StringWriter(); xml = new XmlSerializer(typeof(Complex)); xml.Serialize(writer, complicated); s = writer.ToString(); Console.WriteLine(s); var newComplicated = xml.Deserialize(new StringReader(s)) as Complex; if (newComplicated != null) { Console.WriteLine(); Console.WriteLine("Woo hoo!"); } Console.ReadLine(); } } } </code></pre> <p>This is the output that I got:</p> <pre><code>07 &lt;?xml version="1.0" encoding="utf-16"?&gt; &lt;LeadingZero&gt;07&lt;/LeadingZero&gt; 07 &lt;?xml version="1.0" encoding="utf-16"?&gt; &lt;Complex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http:/ /www.w3.org/2001/XMLSchema"&gt; &lt;ValidFromMonth&gt;07&lt;/ValidFromMonth&gt; &lt;ValidFromYear&gt;2009&lt;/ValidFromYear&gt; &lt;ExpiresEndMonth&gt;06&lt;/ExpiresEndMonth&gt; &lt;ExpiresEndYear&gt;2010&lt;/ExpiresEndYear&gt; &lt;/Complex&gt; Woo hoo! </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload