Xml 列出序列化和节点类型名称

Xml List Serialization and Node Type Names

我在这里遇到了多个问题和答案,但 none 特定于我的情况。

我有一个 class 'Entity' 有多个 class 延伸出来。我希望序列化命中列表并理解和使用每个项目的类型作为节点名称。

现在,我可以使用注释掉的内容(定义主 class 中的每个数组项并使用 [XmlArrayItem("Subclass1", typeof(subclass1)] 但我想将所有定义保留在它们的 subclass 中,我将有太多的 subclasses 来定义主实体中的所有内容 class...是无论如何要实现这个?

我已经尝试使用 [XmlType(TypeName="...")] 作为子 classes 等等,但是没有用。

[Serializable]
[XmlInclude(typeof(Subclass1))]
[XmlRoot("Entity")]
public class Entity{

    [XmlArray("CausedBy")]
    //[XmlArrayItem("Subclass1", typeof(subclass1))]
    //[XmlArrayItem("Sublcass2", typeof(Subclass2))]
    public List<Entity> CausedBy { get; set; }

}

[Serializable]
[XmlRoot("Subclass1")]
[XmlInclude(typeof(Subclass2))]
public class Subclass1:Entity{
    //Code...
}

[Serializable]
[XmlRoot("Subclass2")]
public class Subclass2:Subclass1{
  //Code...
}

在创建实体并将 Subclass1 和 Subclass2 添加到列表后序列化上述代码 'CausedBy' class 结果如下:

<Entity>
  <CausedBy>
    <Entity ... xsi:type="SubClass1" />
    <Entity ... xsi:type="SubClass2" />
   </CausedBy>
<Entity>

我希望输出显示:

 <Entity>
      <CausedBy>
        <SubClass1 .../>
        <SubClass2 .../>
       </CausedBy>
    <Entity>

这对我有用:

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Entity entity = new Entity();
        entity.CausedBy = new List<Entity>();
        entity.CausedBy.Add(new Subclass1());
        entity.CausedBy.Add(new Subclass2());
        entity.CausedBy.Add(new Subclass2());
        entity.CausedBy.Add(new Subclass1());
        entity.CausedBy.Add(new Subclass1());
        entity.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Test.txt"));
    }
}
[Serializable]
[XmlRoot("Entity")]
public class Entity
{
    [XmlArray("CausedBy")]
    [XmlArrayItem("SubClass1", typeof(Subclass1))]
    [XmlArrayItem("SubClass2", typeof(Subclass2))]
    public List<Entity> CausedBy { get; set; }

}

[Serializable]
[XmlRoot("Subclass1")]
public class Subclass1 : Entity
{
    [XmlIgnore]
    String t = DateTime.Now.ToShortDateString();

    public String SubClass1Item { get { return "Test1 " + t; } set { } }
}

[Serializable]
[XmlRoot("Subclass2")]
public class Subclass2 : Entity
{
    [XmlIgnore]
    String t = DateTime.Now.ToString();

    public String SubClass2Item { get { return "Test2 " + t; } set { } }
}

它产生:

<Entity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CausedBy>
    <SubClass1>
      <SubClass1Item>Test1 20/09/2017</SubClass1Item>
    </SubClass1>
    <SubClass2>
      <SubClass2Item>Test2 20/09/2017 01:06:55</SubClass2Item>
    </SubClass2>
    <SubClass2>
      <SubClass2Item>Test2 20/09/2017 01:06:55</SubClass2Item>
    </SubClass2>
    <SubClass1>
      <SubClass1Item>Test1 20/09/2017</SubClass1Item>
    </SubClass1>
    <SubClass1>
      <SubClass1Item>Test1 20/09/2017</SubClass1Item>
    </SubClass1>
  </CausedBy>
</Entity>

由于我完全没有读完问题的开头,这里有一个新的答案(有点 tl;dr,所以你总是可以跳到最后并按照 link):

无法使内置序列化程序 class 工作,因为您不希望添加它需要能够运行的属性。您唯一的选择是自己序列化 class,但是,这并不像听起来那么乏味;几年前,我在虚拟模式下遇到了类似的 DataGridView 问题,并生成了一个通用虚拟器,可用于虚拟化显示数据;它使用了自定义属性:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class showColumnAttribute : System.Attribute
{
    ///<summary>Optional display format for column</summary>
    public string Format;
    ///<summary>Optional Header string for column<para>Defaults to propety name</para></summary>
    public string Title;
    ///<summary>Optional column edit flag - defaults to false</summary>
    public bool ReadOnly;
    ///<summary>Optional column width</summary>
    public int Width;
    ///<summary>
    ///Marks public properties that are to be displayed in columns
    ///</summary>
    public showColumnAttribute()
    {
        Format = String.Empty;
        Title = String.Empty;
        ReadOnly = false;
        Width = 0;
    }
}

还有一个构造函数:

    ///<summary>
    ///Extracts the properties of the supplied type that are to be displayed
    ///<para>The type must be a class or an InvalidOperationException will be thrown</para>
    ///</summary>
    public Virtualiser(Type t)
    {
        if (!t.IsClass)
            throw new InvalidOperationException("Supplied type is not a class");

        List<VirtualColumnInfo> definedColumns = new List<VirtualColumnInfo>();
        PropertyInfo[] ps = t.GetProperties();
        MethodInfo mg, ms;

        for (int i = 0; i < ps.Length; i++)
        {
            Object[] attr = ps[i].GetCustomAttributes(true);

            if (attr.Length > 0)
            {
                foreach (var a in attr)
                {
                    showColumnAttribute ca = a as showColumnAttribute;
                    if (ca != null)
                    {
                        mg = ps[i].GetGetMethod();
                        if (mg != null)
                        {
                            ms = ps[i].GetSetMethod();
                            definedColumns.Add
                            (
                                new VirtualColumnInfo
                                (
                                    ps[i].Name, ca.Width, ca.ReadOnly, ca.Title == String.Empty ? ps[i].Name : ca.Title, 
                                    ca.Format, mg, ms
                                )
                            );
                        }
                        break;
                    }
                }
            }
        }
        if (definedColumns.Count > 0)
            columns = definedColumns.ToArray();
    }

这会提取 class 的 public 属性,并将标记的项目作为列与 header、格式等一起提供给 DataGridView。

所有这些(以及其余缺少的代码)的效果是任何类型都可以在 dataGridView 中虚拟化,只需标记 public 属性并为给定类型调用一次虚拟器:

    #region Virtualisation
    static readonly Virtualiser Virtual = new Virtualiser(typeof(UserRecord));
    [XmlIgnore] // just in case!
    public static int ColumnCount { get { return Virtual.ColumnCount; } }
    public static VirtualColumnInfo ColumnInfo(int column)
    {
        return Virtual.ColumnInfo(column);
    }

    public Object GetItem(int column)
    {
        return Virtual.GetItem(column, this);
    }
    /*
    ** The supplied item should be a string - it is up to this method to supply a valid value to the property
    ** setter (this is the simplest place to determine what this is and how it can be derived from a string).
    */
    public void SetItem(int column, Object item)
    {
        String v = item as String;
        int t = 0;
        if (v == null)
            return;
        switch (Virtual.GetColumnPropertyName(column))
        {
            case "DisplayNumber":
                if (!int.TryParse(v, out t))
                    t = 0;

                item = t;
                break;
        }
        try
        {
            Virtual.SetItem(column, this, item);
        }
        catch { }
    }
    #endregion

可以通过创建从 class 数据导出的多个 public 属性来自动指定列数、它们的属性和顺序:

        #region Display columns
    [showColumn(ReadOnly = true, Width = 100, Title = "Identification")]
    public String DisplayIdent
    {
        get
        {
            return ident;
        }
        set
        {
            ident = value;
        }

    }
    [showColumn(Width = 70, Title = "Number on Roll")]
    public int DisplayNumber
    {
        get
        {
            return number;
        }
        set
        {
            number = value;
        }
    }
    [showColumn(Width = -100, Title = "Name")]
    public string DisplayName
    {
        get
        {
            return name == String.Empty ? "??" : name;
        }
        set
        {
            name = value;
        }
    }
    #endregion

这将为 dataGridView 虚拟化任何 class 以显示和编辑数据,多年来我多次使用它,提取要显示的属性正是 XML 序列化所需要的,的确,它有很多相同的特点。

我本来打算采用这种方法来为 XML 序列化做同样的工作,但有人已经在 https://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=474453 做到了,我希望你能利用这种方法来解决你的问题.