Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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#-XML反序列化_C#_Xml_Serialization - Fatal编程技术网

属性的C#-XML反序列化

属性的C#-XML反序列化,c#,xml,serialization,C#,Xml,Serialization,我找不到没有getter的属性不能正确解析的原因,让我给您写一个例子: 用于XML格式 <request> <job mode="modefirst" /> <request> emode被设置为类构造函数中的默认值,当从上面反序列化(System.Xml.Serialization而不使用自定义类,在这里只是尽量简化)Xml时,永远不会调用setter,但是当属性“Mode”包含getter时 get { return this

我找不到没有getter的属性不能正确解析的原因,让我给您写一个例子:

用于XML格式

<request>
  <job 
     mode="modefirst"
  />
<request>
emode被设置为类构造函数中的默认值,当从上面反序列化(System.Xml.Serialization而不使用自定义类,在这里只是尽量简化)Xml时,永远不会调用setter,但是当属性“Mode”包含getter时

    get { return this.emode.ToString(); }
在反序列化过程中,setter实际上被命中并设置了正确的值


为什么会出现这种情况?这背后有什么原因吗?

XmlSerializer只处理具有公共get set访问器的属性。但是您可以通过实现
IXmlSerializable
来定制任何内容:

public class MyXmlSerializableClass : IXmlSerializable
{
    private ESomeEnum emode = ESomeEnum.modefirst;

    public string Mode
    {
        set { emode = ESomeEnum.Parse(value); }
    }

    public int ReadWriteProperty { get; set; }

    public int SemiReadOnlyProperty { get; private set; }

    private int backingFieldOfRealReadOnlyProperty;
    public int RealReadOnlyProperty
    {
        get { return backingFieldOfRealReadOnlyProperty; }
    }

    #region IXmlSerializable Members

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        throw new NotImplementedException();
    }

    public void ReadXml(XmlReader reader)
    {
        if (reader.Settings != null && !reader.Settings.IgnoreWhitespace)
        {
            reader = XmlReader.Create(reader, new XmlReaderSettings { IgnoreWhitespace = true });
            reader.Read();
        }

        reader.ReadStartElement();
        Mode = reader.ReadElementContentAsString("Mode", String.Empty);
        ReadWriteProperty = reader.ReadElementContentAsInt("ReadWriteProperty", String.Empty);
        SemiReadOnlyProperty = reader.ReadElementContentAsInt("ReadOnlyAutoProperty", String.Empty);
        backingFieldOfRealReadOnlyProperty = reader.ReadElementContentAsInt("ReadOnlyProperty", String.Empty);
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteElementString("Mode", emode.ToString());
        writer.WriteElementString("ReadWriteProperty", ReadWriteProperty.ToString(CultureInfo.InvariantCulture));
        writer.WriteElementString("ReadOnlyAutoProperty", SemiReadOnlyProperty.ToString(CultureInfo.InvariantCulture));
        writer.WriteElementString("ReadOnlyProperty", RealReadOnlyProperty.ToString(CultureInfo.InvariantCulture));
    }

    #endregion

    internal MyXmlSerializableClass()
    {/*needed for deserialization*/
    }
}

公共属性必须具有要序列化的getter和setter。简言之,接口的默认配置只反映ReadWriteProperties,我应该先看看源代码。感谢这个很好的例子,我将使用它作为起点。
public class MyXmlSerializableClass : IXmlSerializable
{
    private ESomeEnum emode = ESomeEnum.modefirst;

    public string Mode
    {
        set { emode = ESomeEnum.Parse(value); }
    }

    public int ReadWriteProperty { get; set; }

    public int SemiReadOnlyProperty { get; private set; }

    private int backingFieldOfRealReadOnlyProperty;
    public int RealReadOnlyProperty
    {
        get { return backingFieldOfRealReadOnlyProperty; }
    }

    #region IXmlSerializable Members

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        throw new NotImplementedException();
    }

    public void ReadXml(XmlReader reader)
    {
        if (reader.Settings != null && !reader.Settings.IgnoreWhitespace)
        {
            reader = XmlReader.Create(reader, new XmlReaderSettings { IgnoreWhitespace = true });
            reader.Read();
        }

        reader.ReadStartElement();
        Mode = reader.ReadElementContentAsString("Mode", String.Empty);
        ReadWriteProperty = reader.ReadElementContentAsInt("ReadWriteProperty", String.Empty);
        SemiReadOnlyProperty = reader.ReadElementContentAsInt("ReadOnlyAutoProperty", String.Empty);
        backingFieldOfRealReadOnlyProperty = reader.ReadElementContentAsInt("ReadOnlyProperty", String.Empty);
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteElementString("Mode", emode.ToString());
        writer.WriteElementString("ReadWriteProperty", ReadWriteProperty.ToString(CultureInfo.InvariantCulture));
        writer.WriteElementString("ReadOnlyAutoProperty", SemiReadOnlyProperty.ToString(CultureInfo.InvariantCulture));
        writer.WriteElementString("ReadOnlyProperty", RealReadOnlyProperty.ToString(CultureInfo.InvariantCulture));
    }

    #endregion

    internal MyXmlSerializableClass()
    {/*needed for deserialization*/
    }
}