1

trying to deserialize a Xml string, but always get problem for elements like these:

<Taxable />
<DefaultPurchasePrice />

My C# code snippet:

[XmlRoot(ElementName = "Product", Namespace = "http://api.test.com/version/1", IsNullable = false)]
public class Product
{
    public Guid Guid { get; set; }
    public string ProductName { get; set; }
    public bool Taxable { get; set; }
    public Decimal DefautSellPrice { get; set; }
    [XmlElement("DefaultPurchasePrice")]
    public string DefaultPurchasePriceElement
    {
        get
        {
            if (DefaultPurchasePrice == null)
                return String.Empty;
            else
                return DefaultPurchasePrice.ToString();
        }
        set
        {
            if (value == null | value.Length == 0)
                DefaultPurchasePrice = null;
            else
                DefaultPurchasePrice = Convert.ToDecimal(value);
        }
    }

    [XmlIgnore]
    public decimal? DefaultPurchasePrice{ get; set;}
}

Seems like

xsi:nil="true"

attribute in XML should solve my problem. But as we are using XML provided by from a REST server as part of an API testing. We don't have direct control how the XML be constructed, but we can give them feedback. So I think I should explicitly ask them to fix their XML, as it is their XML's problem right?

In the mean time, I could get individual elements deserialized by the following code:

[XmlElement("DefaultPurchasePrice")]
public string DefaultPurchasePriceElement
{
    get
    {
        if (DefaultPurchasePrice == null)
             return String.Empty;
        else
             return DefaultPurchasePrice.ToString();
     }
     set
     {
         if (value == null | value.Length == 0)
              DefaultPurchasePrice = null;
         else
              DefaultPurchasePrice = Convert.ToDecimal(value);
      }
  }

[XmlIgnore]
public decimal? DefaultPurchasePrice{ get; set;}

But there are quite a few null elements in the XML string, and again, the other party could fix their XML so I don't need do anything to my deserialize code in that case right?

Anyway, could I do something in my code before deserialization so the XML could have proper xsi:nil="true" attribute for null elements so that I don't need do much in my C# code but can quickly fix their XML?

I am thinking about @Ryan's solution in the 2nd last from here: deserialize-xml-with-empty-elements-in-c, but not sure are there any better solutions?

EDIT:

Just did a small test, adding xsi:nill='true' in XML null elements will indeed working with my existing C# code. But I do need make sure my C# class mapped from XML have nullable datattype for those null elements comeing from XML with xsi:nill='true'. But it make sense: when some datafield come from XML might be a null type, I need explicitly define the correspond datatype as nullable. I am much happy with that rather than my current solution.

Community
  • 1
  • 1
Paul L
  • 2,240
  • 5
  • 36
  • 55

1 Answers1

0

I don't know the answer to your problem, but it seems to me that asking your colleagues to fix their XML isn't the right answer. It is common wisdom when writing network and file format code to "Be conservative in what you give, but accepting in what you receive", or some such.

That is, you should be prepared to receive just about ANYTHING in your incoming XML stream. If the XML is well-formed and contains the elements and attributes you require, you should be able to parse it correctly. If it has elements you don't permit, you should gracefully either ignore them or raise an error condition. If the XML is not well-formed, you should raise an error.

Otherwise your program won't be robust in the face of errors coming in from the other end, and could have security holes.

Mike Crawford
  • 2,232
  • 2
  • 18
  • 28
  • Hi @Don Quixote, in general I tend to agree with you. But in this case, I believe that they provide semantically correct XML, but not necessary in correct format. I could either cleaN their XMN in my code, which is I am planing doing, or modify my C# class to handle that, which will works, but involve little more work. Or I could ask them to change the XML so it will make it easier for us and probably other developers to work with their API. So yes, I do have a choice, and if I can choose, it will be work smarter, not harder. – Paul L Aug 30 '11 at 02:23
  • For reference, see this: http://stackoverflow.com/questions/774192/what-is-the-correct-way-to-represent-null-xml-elements to see why I think their XML didn't represent null element in properly format. – Paul L Aug 30 '11 at 02:25