C# 将类序列化为XML,基于属性动态设置元素名称,基于其他属性动态设置xmltext

C# 将类序列化为XML,基于属性动态设置元素名称,基于其他属性动态设置xmltext,c#,c#-4.0,serialization,visual-studio-2013,xml-serialization,C#,C# 4.0,Serialization,Visual Studio 2013,Xml Serialization,是否可以将下面的类序列化为XML,并生成此输出 挑战在于元素doc_id和client_name应该基于密钥动态设置,在本例中,doc_id和client_name是基于密钥的。在本例中,对应的值应该放在元素的文本中,即1、客户机1、12和客户机2 我可以重写这些类,但我必须生成这个XML <row> <doc_id>00000001</doc_id> <client_name>Client 1</client_name> &l

是否可以将下面的类序列化为XML,并生成此输出

挑战在于元素doc_id和client_name应该基于密钥动态设置,在本例中,doc_id和client_name是基于密钥的。在本例中,对应的值应该放在元素的文本中,即1、客户机1、12和客户机2

我可以重写这些类,但我必须生成这个XML

<row>
  <doc_id>00000001</doc_id>
  <client_name>Client 1</client_name>
</row>
<row>
  <doc_id>000000012</doc_id>
  <client_name>Client 2</client_name>
</row>

    public struct SerializableKeyValuePair<T1, T2>
    {
        public T1 Key;

        public T2 Value;

        public SerializableKeyValuePair(T1 key, T2 value)
        {
            Key = key;
            Value = value;
        }
    }

    public class ActionParameter
    {
    [XmlElement("Row")]
        public List<SerializableKeyValuePair<object, object>> Rows;
    }

不幸的是,如果不编写自己的序列化程序,或者只是手动处理读/写操作,就无法实现

尝试使用预先制作的序列化程序是行不通的,因为它无法知道什么是真正的键,什么是值

但是,仍然可以将其与标准XmlSerializer结合使用


这基本上是我快速编写的伪代码,旨在向您展示一种可能的方法。

不太可能。如果你想定制这样的结构,你需要手动处理写/读操作。即使你可以,我也认为序列化程序实际上不可能知道如何反序列化它。令人失望。。。但这是有道理的。你想在回答者添加答案时发布吗。前几天我本打算这么做的,但我完全忘记了。迟到总比不做好
public class ActionParameter
{
    [XmlElement("Row")]
    public List<XmlElement> RowElements
    {
        // The `get` is purely for the serializer. No reason for you to call it.
        get { return SomeMethodToSerializeToXmlDoc(Rows).ChildNodes.OfType<XmlElement>()
                                                                   .ToList(); }
        // The `set` from the Serializer will allow your "Rows" to be Deserialized.
        set { DeserializeKeyValues(value); }
    }

    [XmlIgnore]
    public List<SerializableKeyValuePair<string, string>> Rows { get; set; }

    void DeserializeKeyValues(List<XmlElement> elements) 
    {
         Rows = new List<SerializableKeyValuePair<string, string>>();
         foreach(XmlElement element in elements)
         {
             // Shouldn't your List of Rows, actually be another List of Key Values?
             // This call won't actually work. I think you need to really
             // loop through the children of this element as your key values.
             Rows.Add(new SerializableKeyValuePair<string, string>(element));
         }
    }
}
public struct SerializableKeyValuePair<T1, T2>
{
    public T1 Key;

    public T2 Value;

    public SerializableKeyValuePair(T1 key, T2 value)
    {
        Key = key;
        Value = value;
    }

    public SerializableKeyValuePair(XmlElement element)
    {
        Key = element.LocalName;
        var textNode = element.SelectSingleNode("text()");
        // Here you are also not limited to just text, since you have an actual Element, 
        // you could attempt to try and implement deserializing custom classes/objects.
        if (textNode != null)
            Value = SomeConvertForT2(textNode.Value);
    }
}