gpt4 book ai didi

c# - 将 XML 反序列化为对象 : XML element with xsi:nil ="true" should have null value (and not empty value) for respective property in object

转载 作者:太空宇宙 更新时间:2023-11-03 15:00:12 24 4
gpt4 key购买 nike

将以下 XML 反序列化为父类时,ElementTwo 和 ElementThree 为空字符串,这是预期的。但是 ElementOne 应该是空的,但它也是空字符串。

What does i:nil="true" mean?

XML

<?xml version = \"1.0\" ?>
<Parent
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ElementOne xsi:nil="true"/>
<ElementTwo></ElementTwo>
<ElementThree />
<ElementFour>Value</ElementFour>
</Parent>

C# 类

public class Parent
{
public string ElementOne { get; set; }
public string ElementTwo { get; set; }
public string ElementThree { get; set; }
public string ElementFour { get; set; }
}

将 XML 反序列化为对象时,xsi:nil="true"的 XML 元素不会被转换为 null。相反,它被分配为空字符串。但我有一个要求,它应该只转换为 null。请帮我找出解决方案或指出我出错的地方

我已经给出了下面 fiddle 链接中使用的示例:

https://dotnetfiddle.net/VfNJYv

最佳答案

[XmlElement(IsNullable=true)] 

在上面公共(public)字符串 ElementOne 获取/设置属性

.NET fiddle

关于c# - 将 XML 反序列化为对象 : XML element with xsi:nil ="true" should have null value (and not empty value) for respective property in object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46585067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com