LinqToXml does not handle nillable elements as expected

后端 未结 3 1783
后悔当初
后悔当初 2020-12-20 00:32

According to W3C standards, if you have a nillable element with a nil value, you are supposed to format it like this:



        
3条回答
  •  佛祖请我去吃肉
    2020-12-20 01:08

    You could also do something like this, taking advantage of the null coalescing operator:

    public static object Nil
    {
        get
        {
            // **I took a guess at the syntax here - you should double check.**
            return new XAttribute(Xsi + "nil", true);
        }
    }
    
    // ......
    
    object nullableContent = ...;
    element.Add(
        new XElement(NS + "myNillableElement", nullableContent ?? Nil)
        );
    

提交回复
热议问题