本文介绍了在System.Xml.Serialization中以字符串形式构建自定义序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有2个这样的课程:

Hi guys I have 2 classes like this:

[XmlRoot("Config")]
public class ConfigClass
{
    [XmlElement("Configuration1")]
    public string Config1 { get; set; }

    [XmlArray("Infos")]
    [XmlArrayItem("Info")]
    public OtherInfo[] OtherInfos { get; set; }
}

public class OtherInfo
{
    public string Info1 { get; set; }
    public string Info2 { get; set; }
}

当我序列化根类时,XML结果如下:

When I serialize the root class the XML result is like this:

<?xml version="1.0"?>
<Config>
  <Configuration1>Text</Configuration1>
  <Infos>
    <Info>
      <Info1>Test 2</Info1>
      <Info2>Text 3</Info2>
    </Info>
    <Info>
      <Info1>Test 4</Info1>
      <Info2>Text 5</Info2>
    </Info>
  </Infos>
</Config>

但是我想将OtherInfo类序列化为这样的单个字符串:

But I would like the OtherInfo class was serialized as a single string like this:

<?xml version="1.0"?>
<Config>
  <Configuration1>Text</Configuration1>
  <Infos>
    <Info>
      Test 2:Text 3
    </Info>
    <Info>
      Test 4:Text 5
    </Info>
  </Infos>
</Config>

我该怎么做?

推荐答案

您可以实现 IXmlSerializable接口,因此Info1Info2属性以<Info>Info1:Info2</Info>:

You can implement the IXmlSerializable interface, so the Info1 and Info2 properties are serialized the way <Info>Info1:Info2</Info>:

public class OtherInfo: IXmlSerializable
{
    public string Info1 { get; set; }
    public string Info2 { get; set; }

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

    public void ReadXml(System.Xml.XmlReader reader)
    {
        var content = reader.ReadElementContentAsString();

        if (String.IsNullOrWhiteSpace(content))
            return;

        var infos = content.Split(':');
        if (infos.Length < 2)
            return;

        this.Info1 = infos[0];
        this.Info2 = infos[1];
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteString(String.Format("{0}:{1}", this.Info1, this.Info2));
    }
}

如果在应用程序内部还需要具有"Info1:Info2"格式的那些属性,而不仅仅是Xml序列化,那么您可以在OtherInfo中具有如下所示的属性,并在序列化中隐藏Info1和Info2:

If having those properties in the format "Info1:Info2" is also needed inside the application and not just for Xml serialization, then you could have a property in OtherInfo like the following and hide Info1 and Info2 from the serialization:

public class OtherInfo
{
    [XmlIgnore]
    public string Info1 { get; set; }
    [XmlIgnore]
    public string Info2 { get; set; }

    [XmlText]
    public string InfoString
    {
        get
        {
            return String.Format("{0}:{1}", this.Info1, this.Info2);
        }
        set
        {
            if (String.IsNullOrWhiteSpace(value))
                return;

            var infos = value.Split(':');
            if (infos.Length < 2)
                return;

            this.Info1 = infos[0];
            this.Info2 = infos[1];
        }
    }
}

这篇关于在System.Xml.Serialization中以字符串形式构建自定义序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:41