简体   繁体   English

XML反序列化null元素?

[英]XML deserialize null elements?

trying to deserialize a Xml string, but always get problem for elements like these: 试图反序列化Xml字符串,但总是遇到类似以下元素的问题:

<Taxable />
<DefaultPurchasePrice />

My C# code snippet: 我的C#代码段:

[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" xsi:nil =“ true”

attribute in XML should solve my problem. XML属性应该可以解决我的问题。 But as we are using XML provided by from a REST server as part of an API testing. 但是,由于我们正在使用REST服务器提供的XML作为API测试的一部分。 We don't have direct control how the XML be constructed, but we can give them feedback. 我们没有直接控制XML的构造方式,但是我们可以给他们反馈。 So I think I should explicitly ask them to fix their XML, as it is their XML's problem right? 因此,我认为我应该明确要求他们修复XML,因为这是XML的问题吧?

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? 但是XML字符串中有很多null元素,而且,另一方可以修复其XML,因此在这种情况下我不需要对反序列化代码做任何事情,对吗?

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? 无论如何,我可以在反序列化之前在代码中做些什么,以便XML可以为null元素提供适当的xsi:nil =“ true”属性,这样我就不需要在C#代码中做很多事情,但是可以快速修复其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? 我正在从这里倒数第二个考虑@Ryan的解决方案: deserialize-xml-with-empty-elements-in-c ,但不确定是否有更好的解决方案?

EDIT: 编辑:

Just did a small test, adding xsi:nill='true' in XML null elements will indeed working with my existing C# code. 只是做了一个小测试,在XML空元素中添加xsi:nill ='true'确实可以与我现有的C#代码一起使用。 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'. 但是我确实需要确保从XML映射的C#类具有可空的datattype,以用于那些来自XML且带有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. 但这是有道理的:当来自XML的某些数据字段可能是null类型时,我需要将对应的数据类型显式定义为nullable。 I am much happy with that rather than my current solution. 我对此感到非常满意,而不是我当前的解决方案。

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. 我不知道您问题的答案,但在我看来,要求您的同事修复XML并不是正确的答案。 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. 也就是说,您应该准备好在传入的XML流中接收几乎所有内容。 If the XML is well-formed and contains the elements and attributes you require, you should be able to parse it correctly. 如果XML格式正确,并且包含所需的元素和属性,则应该能够正确解析它。 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. 如果XML格式不正确,则应该引发错误。

Otherwise your program won't be robust in the face of errors coming in from the other end, and could have security holes. 否则,面对来自另一端的错误,您的程序将不会很健壮,并且可能会有安全漏洞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM