Bee Eee Blog

c# Getting a nullable attribute to serialize to xml

by brian on Jul.23, 2009, under .NET, c#, c# coding GUI, coding, linq

It isn’t straight forward to get a nullable field to serialize. Technically it isn’t supported. But I just discovered a way today. You see null strings are supported. The idea is to ignore the real nullable property while substituting the string version as follows:

    [XmlRoot("Comment")]
    public class Comment
    {
        [XmlIgnore]
        public DateTime? CommentTime { get; set; }

        [XmlAttribute("CommentTime")]
        public string CommentTimeStr
        {
            get
            {
                return (CommentTime.HasValue) ? CommentTime.Value.ToString() : null;
            }

            set
            {
                if (string.IsNullOrEmpty(value))
                    CommentTime = null;
                else
                    CommentTime = DateTime.Parse(value);
            }
        }
  }

Now it can be serialized.

102 comments for this entry:
  1. JOHNNIE


    CheapTabletsOnline.com. Canadian Health&Care.Best quality drugs.Special Internet Prices.No prescription online pharmacy. No prescription pills. Order drugs online

    Buy:Arimidex.Lumigan.Human Growth Hormone.Mega Hoodia.Nexium.Zovirax.Synthroid.Valtrex.Zyban.100% Pure Okinawan Coral Calcium.Petcam (Metacam) Oral Suspension.Prevacid.Prednisolone.Actos.Accutane.Retin-A….

  2. WESLEY


    NEW FASHION store. Original designers collection at low prices!!! 20 % TO 70 % OFF. END OF SEASON SALE!!!

    BUY FASHION. TOP BRANDS: GUCCI, DOLCE&GABBANA, BURBERRY, DIESEL, ICEBERG, ROBERTO CAVALLI, EMPORIO ARMANI, VERSACE…

  3. JESUS

    ████████►BUY VIAGRA◀███████…

    ████████▲▲▲▲▲▲▲▲▲████████…

  4. RALPH

    Aleve@official.site” rel=”nofollow”>..

    Buyit now…

  5. SERGIO

    aloe@vera.gel.indicatii” rel=”nofollow”>…

    Buygeneric drugs…

  6. FERNANDO

    Amoxicillin@official.site” rel=”nofollow”>…

    Buyno prescription…

  7. JESSIE

    avodart@dosage.now” rel=”nofollow”>..

    Buygeneric meds…

  8. GREGORY

    Azor@official.site” rel=”nofollow”>…

    Buygeneric pills…

  9. ENRIQUE

    Aricept@official.site” rel=”nofollow”>.

    Buynow…

  10. ALBERTO

    Crestor@official.site” rel=”nofollow”>.

    Buywithout prescription…

  11. JORDAN

    < a href="http://my.gardenguides.com/members/buy-Cialis? buy@Cialis.now

    Buydrugs without prescription…

  12. GUY

    abilify@and.sleep.issues” rel=”nofollow”>.

    Buyno prescription…

  13. RON

    cheap@Viagra.now” rel=”nofollow”>…

    Buygeneric pills…

  14. ANDREW

    Risperdal@official.site” rel=”nofollow”>..

    Buynow it…

  15. SEAN

    Zyrtec@official.site” rel=”nofollow”>.

    Buynow…

  16. RONALD

    Depakote@official.site” rel=”nofollow”>.

    Buygeneric drugs…

  17. EDWARD

    zyban pill

    Buywithout prescription…

  18. VIRGIL

    Zetia

    Buyno prescription…

  19. RUSSELL

    Zofran

    Buygeneric pills…

  20. REX

    Lexapro

    Buygeneric drugs…

  21. SHAUN

    Omnicef

    Buywithout prescription…

  22. ELMER

    Rogaine

    Buygeneric meds…

  23. HUBERT

    Prozac

    Buyno prescription…

  24. CHESTER

    Prozac

    Buygeneric meds…

  25. DONNIE

    prozac breast feeding

    Buydrugs without prescription…

  26. CLIFTON

    Paxil

    Buygeneric drugs…

  27. TERRANCE

    Pyrantel@Pamoate.now” rel=”nofollow”>..

    Buygeneric pills…

  28. JONATHAN

    Pulmicort@Pulmicort.Pulmicort” rel=”nofollow”>.

    Buynow it…

  29. WARREN

    Purim@Purim.Purim” rel=”nofollow”>..

    Buynow…

  30. CARL

    Skelaxin@Skelaxin.Skelaxin” rel=”nofollow”>..

    Buyit now…

  31. PAUL

    symbicort@official.us.website” rel=”nofollow”>…

    Buygeneric drugs…

  32. JAIME

    Cytoxan@Cytoxan.Cytoxan” rel=”nofollow”>..

    Buydrugs without prescription…

  33. RICKY

    Seroquel@Seroquel.Seroquel” rel=”nofollow”>..

    Buynow it…

  34. GREGORY

    Provera@Provera.Provera” rel=”nofollow”>.

    Buygeneric pills…

  35. LLOYD

    risperdal@warnings.for.elderly” rel=”nofollow”>…

    Buynow it…

  36. WILLIE

    Coumadin@Coumadin.Coumadin” rel=”nofollow”>.

    Buyno prescription…

  37. DARREN

    Prozac@Prozac.Prozac” rel=”nofollow”>.

    Buygeneric drugs…

  38. JOSHUA

    Cipro@Cipro.Cipro” rel=”nofollow”>..

    Buygeneric meds…

  39. WILLARD

    Seroquel@Seroquel.Seroquel” rel=”nofollow”>..

    Buywithout prescription…

  40. ERNESTO

    Pulmicort@Pulmicort.Pulmicort” rel=”nofollow”>..

    Buygeneric meds…

  41. ALBERT

    Seroquel@Seroquel.Seroquel” rel=”nofollow”>…

    Buydrugs without prescription…

  42. OLIVER

    coral@calcium.ultra” rel=”nofollow”>…

    Buygeneric drugs…

  43. JAIME

    buy@human.growth.hormone.product” rel=”nofollow”>…

    Buygeneric drugs…

  44. SHAUN

    cms@coverage.of.pulmicort.respules.j7627” rel=”nofollow”>.

    Buygeneric pills…

  45. DUSTIN

    early@childhood.purim.crafts” rel=”nofollow”>.

    Buygeneric drugs…

  46. DENNIS

    dapple@hydrocortisone.cream” rel=”nofollow”>…

    Buyit now…

  47. FRANKLIN

    .

    Buygeneric pills…

  48. DAN

    Buygeneric drugs…

  49. RAFAEL

    pyridium@can.cause.low.blood.sugar” rel=”nofollow”>.

    Buygeneric drugs…

  50. JACOB

    celexa@and.mda” rel=”nofollow”>…

    Buygeneric drugs…

  51. LEE

    reglan@black.box” rel=”nofollow”>…

    Buydrugs without prescription…

  52. ARNOLD

    cipro@causes.problems” rel=”nofollow”>.

    Buygeneric drugs…

  53. TERRY

    claritin@helps.poison.ivy” rel=”nofollow”>…

    Buydrugs without prescription…

  54. KURT

    buy@acai.berry.supplement” rel=”nofollow”>.

    Buyit now…

  55. JORDAN

    .

    Buywithout prescription…

  56. WAYNE

    accutane@and.ototoxicity” rel=”nofollow”>…

    Buyno prescription…

  57. THOMAS

    retrovir@for.cfs” rel=”nofollow”>..

    Buygeneric meds…

  58. IVAN

    difference@between.medrol.and.prednisolone” rel=”nofollow”>…

    Buywithout prescription…

  59. FRANKLIN

    effexor@drug.interaction” rel=”nofollow”>.

    Buygeneric pills…

  60. LEE

    is@prometrium.and.blood.clots” rel=”nofollow”>..

    Buywithout prescription…

  61. SHAUN

    cutting@proscar.tablets” rel=”nofollow”>.

    Buygeneric pills…

  62. HARRY

    .

    Buygeneric drugs…

  63. CASEY

    buy@online.amoxicillin” rel=”nofollow”>.

    Buywithout prescription…

  64. EVERETT

    otc@nasonex.alternative” rel=”nofollow”>…

    Buynow…

  65. LANCE

    gout@drugs.arcoxia” rel=”nofollow”>.

    Buygeneric pills…

  66. ROLAND

    best@ay.to.take.extenze” rel=”nofollow”>…

    Buyit now…

  67. REX

    coral@calcium.safety” rel=”nofollow”>.

    Buyit now…

  68. NORMAN

    changing@from.paxil.to.paxil.cr” rel=”nofollow”>.

    Buyno prescription…

  69. JORDAN

    plan@b.skateboards.at.target” rel=”nofollow”>.

    Buywithout prescription…

  70. JEROME

    abilify@does.it.work” rel=”nofollow”>.

    Buynow it…

  71. EDDIE

    calories@and.nutrition.in.boost.nutritional.energy.drink” rel=”nofollow”>…

    Buywithout prescription…

  72. KURT

    livingston@foot.care.bellmore” rel=”nofollow”>.

    Buyit now…

  73. Alexander7

    buy@generic.LEVITRA” rel=”nofollow”>…

    Need cheap generic LEVITRA?…

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...