XML反序列化空元素? [英] XML deserialize null elements?

查看:104
本文介绍了XML反序列化空元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图反序列化一个 Xml 字符串,但总是遇到以下元素的问题:

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

<Taxable />
<DefaultPurchasePrice />

我的 C# 代码片段:

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;}
}

好像是

xsi:nil="true"

xsi:nil="true"

XML 中的属性应该可以解决我的问题.但是因为我们使用 REST 服务器提供的 XML 作为 API 测试的一部分.我们无法直接控制 XML 的构造方式,但我们可以向他们提供反馈.所以我认为我应该明确要求他们修复他们的 XML,因为这是他们的 XML 的问题,对吗?

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;}

但是 XML 字符串中有很多空元素,而且对方可以修复他们的 XML,所以在这种情况下我不需要对我的反序列化代码做任何事情,对吗?

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 可以为空元素提供适当的 xsi:nil="true" 属性,这样我就不需要在 C# 代码中做太多事情,但可以快速修复它们的 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?

我正在考虑从这里倒数第二个@Ryan的解决方案:反序列化-xml-with-empty-elements-in-c,但不确定是否有更好的解决方案?

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?

刚刚做了一个小测试,在 XML 空元素中添加 xsi:nill='true' 确实可以与我现有的 C# 代码一起使用.但我确实需要确保从 XML 映射的 C# 类对于那些来自 XML 且 xsi:nill='true' 的空元素具有可为空的数据类型.但这是有道理的:当来自 XML 的某些数据字段可能是空类型时,我需要将相应的数据类型明确定义为可为空.我对此感到非常满意,而不是我目前的解决方案.

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.

推荐答案

我不知道您的问题的答案,但在我看来,要求您的同事修复他们的 XML 并不是正确的答案.在编写网络和文件格式代码时,给予的要保守,收到的要接受",或者诸如此类.

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.

也就是说,您应该准备好接收传入 XML 流中的任何内容.如果 XML 格式正确并且包含您需要的元素和属性,您应该能够正确解析它.如果它包含您不允许的元素,您应该优雅地忽略它们或引发错误条件.如果 XML 格式不正确,则应引发错误.

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.

这篇关于XML反序列化空元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆