在反序列化XML时为属性设置默认值

时间:2011-11-10 09:27:49

标签: c# xml xml-deserialization

我有一个xml文件,我试图反序列化。 我正在实现用于序列化和反序列化的IXmlSerializable接口。 我没有看到序列化有任何问题。 但随着反序列化,我几乎没有问题。 1)。我正在使用xml reader的'ReadToFollowing'来读取元素。通过这种方法,我必须按元素的顺序读取值。如果订单中存在任何不匹配,我将无法读取remainingig值。 2)。根据我的要求,我必须在反序列化xml时提供向后兼容性。因此,当我加载一些旧版本的xml文件(可能不包含所有元素作为最新版本的xml)时,'ReadToFollowing'抛出异常。

最新版本Xml

<LEFTSECTION>
        <DATA />
        <FONTNAME>Arial</FONTNAME>
        <FONTSTYLE>Regular</FONTSTYLE>
        <FONTSIZE>10</FONTSIZE>
        <TEXTCOLOR>-16777216</TEXTCOLOR>
        <STRIKEOUT>0</STRIKEOUT>
        <UNDERLINE>0</UNDERLINE>
        <BORDER>0</BORDER>
        <IMAGE>0</IMAGE>
        <IMAGENAME />
        <ALIGNMENT>4</ALIGNMENT>
        <SECTIONHEIGHT>0.5454546</SECTIONHEIGHT>
        <SECTIONWIDTH>0.33</SECTIONWIDTH>
      </LEFTSECTION>

旧版Xml

<LEFTSECTION>
        <DATA>asas#APP_OEM_NAME#</DATA>
        <FONTNAME>Arial Unicode MS</FONTNAME>
        <FONTSTYLE>Regular</FONTSTYLE>
        <FONTSIZE>10</FONTSIZE>
        <TEXTCOLOR>-16777216</TEXTCOLOR>
        <STRIKEOUT>0</STRIKEOUT>
        <UNDERLINE>0</UNDERLINE>
        <BORDER>0</BORDER>
        <IMAGE>0</IMAGE>
        <IMAGENAME>
        </IMAGENAME>
      </LEFTSECTION>

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

好。我有一个游戏,这就是我想出来的:

Serializableable class:

[Serializable]
[XmlRoot("LEFTSECTION")]
public class NewClass
{
    [XmlElement("DATA")]
    [System.ComponentModel.DefaultValueAttribute("")]
    public string Data;

    [XmlElement("FONTNAME")]
    public string FontName;

    [XmlElement("FONTSTYLE")]
    public string FontStyle;

    [XmlElement("FONTSIZE")]
    public int FontSize;

    [XmlElement("TEXTCOLOR")]
    public int TextColor;

    [XmlElement("STRIKEOUT")]
    public int Strikeout;

    [XmlElement("UNDERLINE")]
    public int Underline;

    [XmlElement("BORDER")]
    public int Border;

    [XmlElement("IMAGE")]
    public int Image;

    [XmlElement("IMAGENAME")]
    public string ImageName;

    [System.ComponentModel.DefaultValue(0)]
    [XmlElement("ALIGNMENT")]
    public int Alignment;

    [XmlElement("SECTIONHEIGHT")]
    public double SectionHeight;

    [XmlElement("SECTIONWIDTH")]
    public double SectionWidth;
}

然后在我的测试程序中编码:

    NewClass test = new NewClass();
    XmlSerializer serializer = new XmlSerializer(typeof(NewClass));

    FileStream file = new FileStream("input.xml", FileMode.Open);
    test = (NewClass) serializer.Deserialize(file);
    file.Close();

    file = new FileStream("old.xml", FileMode.Open);
    test = (NewClass)serializer.Deserialize(file);
    file.Close();

input.xml的内容:

<?xml version="1.0" encoding="utf-8" ?>
<LEFTSECTION>
  <DATA />
  <FONTNAME>Arial</FONTNAME>
  <FONTSTYLE>Regular</FONTSTYLE>
  <FONTSIZE>10</FONTSIZE>
  <TEXTCOLOR>-16777216</TEXTCOLOR>
  <STRIKEOUT>0</STRIKEOUT>
  <UNDERLINE>0</UNDERLINE>
  <BORDER>0</BORDER>
  <IMAGE>0</IMAGE>
  <IMAGENAME />
  <ALIGNMENT>4</ALIGNMENT>
  <SECTIONHEIGHT>0.5454546</SECTIONHEIGHT>
  <SECTIONWIDTH>0.33</SECTIONWIDTH>
</LEFTSECTION>

old.xml的内容:

<LEFTSECTION>
  <DATA>asas#APP_OEM_NAME#</DATA>
  <FONTNAME>Arial Unicode MS</FONTNAME>
  <FONTSTYLE>Regular</FONTSTYLE>
  <FONTSIZE>10</FONTSIZE>
  <TEXTCOLOR>-16777216</TEXTCOLOR>
  <STRIKEOUT>0</STRIKEOUT>
  <UNDERLINE>0</UNDERLINE>
  <BORDER>0</BORDER>
  <IMAGE>0</IMAGE>
  <IMAGENAME>
  </IMAGENAME>
</LEFTSECTION>

这会正确填充我的课程。请注意,在我的类的DataAlignment属性上,如果它们不存在,我会设置默认值。它们也都是从文件中的内容重命名的。

我希望这会有所帮助。

修改

啊,我明白了,你们已经遇到了类的IXmlSerializable方法。

试试这个并不漂亮,但似乎有效:

这是我的IXMLSerializable类:

   public class XmlSerializableNewClass : IXmlSerializable
    {
        public string Data;
        public string FontName;
        public string FontStyle;
        public int FontSize;
        public int TextColor;
        public int Strikeout;
        public int Underline;
        public int Border;
        public int Image;
        public string ImageName;
        public int Alignment;
        public double SectionHeight;
        public double SectionWidth;
        public string[]elementNames={"DATA", "FONTNAME", "FONTSTYLE","FONTSIZE","TEXTCOLOR","STRIKEOUT", "UNDERLINE", "BORDER", "IMAGE", "IMAGENAME", "ALIGNMENT", "SECTIONHEIGHT", "SECTIONWIDTH"};

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            //set default values
            Data=string.Empty;
            FontName = string.Empty;
            FontStyle = string.Empty;
            FontSize = 0;
            TextColor = 0;
            Strikeout = 0;
            Underline = 0;
            Border = 0;
            Image = 0;
            ImageName = string.Empty;
            Alignment = 0;
            SectionHeight = 0.0;
            SectionWidth = 0.0;

            reader.MoveToContent();

            Boolean isEmptyElement= false;
            isEmptyElement = reader.IsEmptyElement;
            reader.ReadStartElement();
            for (int i=0; i< elementNames.Length; i++)
            {
                isEmptyElement = reader.IsEmptyElement;
                string s = reader.Name;
                switch (s)
                {
                    case "DATA":
                        if (!isEmptyElement)
                        {
                            Data = reader.ReadElementString("DATA");
                        }
                        else
                        {
                            Data = string.Empty;
                            reader.ReadStartElement();
                        }
                        break;
                    case "FONTNAME":
                        if (!isEmptyElement)
                        {
                            FontName = reader.ReadElementString("FONTNAME");
                        }
                        else
                        {
                            FontName = string.Empty;
                            reader.ReadStartElement();
                        }
                        break;
                    case "FONTSTYLE":
                        if (!isEmptyElement)
                        {
                            FontStyle = reader.ReadElementString("FONTSTYLE");
                        }
                        else
                        {
                            FontStyle = string.Empty;
                            reader.ReadStartElement();
                        }
                        break;
                    case "FONTSIZE":
                        if (!isEmptyElement)
                        {
                            FontSize = reader.ReadElementContentAsInt();
                        }
                        else
                        {
                            FontSize = 0;
                            reader.ReadEndElement();
                        }
                        break;
                    case "TEXTCOLOR":
                        if (!isEmptyElement)
                        {
                            TextColor = reader.ReadElementContentAsInt();
                        }
                        else
                        {
                            TextColor = 0;
                            reader.ReadStartElement();
                        }
                        break;
                    case "STRIKEOUT":
                        if (!isEmptyElement)
                        {
                            Strikeout = reader.ReadElementContentAsInt();
                        }
                        else
                        {
                            Strikeout = 0;
                            reader.ReadStartElement();
                        }
                        break;
                    case "UNDERLINE":
                        if (!isEmptyElement)
                        {
                            Underline = reader.ReadElementContentAsInt();
                        }
                        else
                        {
                            Underline = 0;
                            reader.ReadStartElement();
                        }
                        break;
                    case "BORDER":
                        if (!isEmptyElement)
                        {
                            Border = reader.ReadElementContentAsInt();
                        }
                        else
                        {
                            Border = 0;
                            reader.ReadStartElement();
                        }
                        break;
                    case "IMAGE":
                        if (!isEmptyElement)
                        {
                            Image = reader.ReadElementContentAsInt();
                        }
                        else
                        {
                            Image = 0;
                            reader.ReadStartElement();
                        }
                        break;
                    case "IMAGENAME":
                        if (!isEmptyElement)
                        {
                            ImageName = reader.ReadElementString("IMAGENAME");
                        }
                        else
                        {
                            ImageName = string.Empty;
                            reader.ReadStartElement();
                        }
                        break;
                    case "ALIGNMENT":
                        if (!isEmptyElement)
                        {
                            Alignment = reader.ReadElementContentAsInt();
                        }
                        else
                        {
                            Alignment = 0;
                            reader.ReadStartElement();
                        }
                        break;
                    case "SECTIONHEIGHT":
                        if (!isEmptyElement)
                        {
                            SectionHeight = reader.ReadElementContentAsDouble();
                        }
                        else
                        {
                            SectionHeight = 0;
                            reader.ReadStartElement();
                        }
                        break;
                    case "SECTIONWIDTH":
                        if (!isEmptyElement)
                        {
                            SectionWidth = reader.ReadElementContentAsDouble();
                        }
                        else
                        {
                            SectionWidth = 0;
                            reader.ReadEndElement();
                        }
                        break;
                }
            }
            reader.ReadEndElement();
       }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

我处理空元素时有点过分了。

这是调用代码,其他一切都是相同的:

    XmlSerializableNewClass test2 = new XmlSerializableNewClass();

    System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
    settings.ConformanceLevel = System.Xml.ConformanceLevel.Fragment;
    settings.IgnoreWhitespace = true;
    settings.IgnoreComments = true;
    System.Xml.XmlReader reader = System.Xml.XmlReader.Create("input.xml", settings);

    test2.ReadXml(reader);

    reader = System.Xml.XmlReader.Create("old.xml", settings);
    test2.ReadXml(reader);