Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中的XmlSerialization中如果为false则隐藏布尔值#_C#_Xml Serialization - Fatal编程技术网

C# 在C中的XmlSerialization中如果为false则隐藏布尔值#

C# 在C中的XmlSerialization中如果为false则隐藏布尔值#,c#,xml-serialization,C#,Xml Serialization,我正在序列化一个类,它有一个boolean参数。如果此参数的值false,我想隐藏它。下面是课堂- public class UserPreferences { [XmlAttributeAttribute("Name")] public string Name; [XmlAttributeAttribute("SaveOnClose")] public bool SaveOnClose; public UserPreferences() { }

我正在序列化一个类,它有一个
boolean
参数。如果此参数的值
false
,我想隐藏它。下面是课堂-

public class UserPreferences
{
    [XmlAttributeAttribute("Name")]
    public string Name;

    [XmlAttributeAttribute("SaveOnClose")]
    public bool SaveOnClose;

    public UserPreferences() { }

    public UserPreferences(string Name)
    {
        this.Name = Name;
    }

    public UserPreferences(string Name, bool SaveOnClose)
    {
        this.Name = Name;
        this.SaveOnClose = SaveOnClose;
    }
}
下面是主要的方法-

UserPreferences userPreferences = new UserPreferences("guest");
XmlSerializer serializer = new XmlSerializer(typeof(UserPreferences));
serializer.Serialize(Console.Out, userPreferences);
下面是这段代码的输出-

<?xml version="1.0" encoding="IBM437"?>
<UserPreferences xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="guest" SaveOnClose="false" />

请注意,参数
SaveOnClose
为false。在这种情况下是否可以隐藏此参数?因为这里的默认值是
false

另外,如果此参数被隐藏并且我反序列化了上述XML,则参数
SaveOnClose
应假定为
false

是,这是可能的

添加一个方法ShouldSerializeSaveOnClose(),如果不应序列化属性SaveOnClose,则让它返回false

public bool ShouldSerializeSaveOnClose() {  
     return SaveOnClose == true;  
 }
[XmlIgnore]
public bool SaveOnCloseSpecified
{
   get { return SaveOnClose == true; }
}
或者,您也可以添加指定的属性,而不是添加方法。 另外,在该属性上添加XmlIgnore属性,以防止它被序列化

public bool ShouldSerializeSaveOnClose() {  
     return SaveOnClose == true;  
 }
[XmlIgnore]
public bool SaveOnCloseSpecified
{
   get { return SaveOnClose == true; }
}

尝试下面的代码:当您在构造函数中指定第二个参数时,该属性将以xml格式返回。否则它就隐藏了

private bool hasDefaultValue = false;

[XmlAttributeAttribute("Name")]
public string Name;

[XmlAttribute("SaveOnClose")]
public bool SaveOnClose;

[XmlIgnore]
public bool SaveOnCloseSpecified
{
    get { return hasDefaultValue; }
}

public UserPreferences() { }

public UserPreferences(string Name)
{
    this.Name = Name;
}

public UserPreferences(string Name, bool SaveOnClose)
{
    this.Name = Name;
    this.SaveOnClose = SaveOnClose;
    hasDefaultValue = true;
}
当您有新的用户首选项(“来宾”,false); 您应该得到如下输出:

<UserPreferences ... Name="guest" SaveOnClose="false" />

对于新用户首选项(“来宾”,true); 您应该得到如下输出:

<UserPreferences ... Name="guest" SaveOnClose="true" />
<UserPreferences ... Name="guest">

对于新用户首选项(“来宾”) 您应该得到如下输出:

<UserPreferences ... Name="guest" SaveOnClose="true" />
<UserPreferences ... Name="guest">

两点可能重复-(1)最后一个方法没有任何返回数据类型。请将其更改为
public bool SaveOnCloseSpecified
(2)对于隐藏的属性,它会给我
false
,如果我反序列化这个生成的XML?您忘了提到一个需要正确理解的重要事实:
SaveOnClose
SaveOnCloseSpecified
之间的关系是通过命名约定自动建立的,因为后缀
Specified
被添加到属性名称中。