Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.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#序列化字典参数,不带父节点_C#_Serialization_Dictionary - Fatal编程技术网

c#序列化字典参数,不带父节点

c#序列化字典参数,不带父节点,c#,serialization,dictionary,C#,Serialization,Dictionary,我有一个类,希望将其序列化为xml。 这个类包含一本字典。。将其切换到可序列化版本(使用writexml/readxml) 问题是当dictionary参数序列化时。。它用父元素“Attributes”包装dictionary元素,我不希望这样 例如: public class Product { public String Identifier{ get; set; } [XmlElement] public SerializableDictionary<stri

我有一个类,希望将其序列化为xml。 这个类包含一本字典。。将其切换到可序列化版本(使用writexml/readxml)

问题是当dictionary参数序列化时。。它用父元素“Attributes”包装dictionary元素,我不希望这样

例如:

public class Product
{
    public String Identifier{ get; set; }
    [XmlElement]
    public SerializableDictionary<string,string> Attributes { get; set; } //custom serializer
}
公共类产品
{
公共字符串标识符{get;set;}
[XmlElement]
public SerializableDictionary属性{get;set;}//自定义序列化程序
}
此产品类被放入列表结构中,然后整个内容被序列化,结果是:

<Products>
<Product>
     <Identifier>12345</Identifier>
     <Attributes>
          <key1> value 1</key1>
          <key2> value 2</key2>
     </Attributes>
</Product>
</Products>

12345
值1
价值2
我不希望有节点包装器

我使用一个序列化的字典类,但通过它的WriteXml,我只能影响键值对。。不是父元素

我可以举出任何自给自足的例子来说明linqpad将非常棒。。 这是可序列化词典的简短版本

   [XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
    #region IXmlSerializable Members
    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        //not including for the sake of brevity
    }
    public void WriteXml(System.Xml.XmlWriter writer)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
        foreach (TKey key in this.Keys)
        {
            writer.WriteStartElement(key.ToString());
            TValue value = this[key];
            if (value == null)
                writer.WriteValue(String.Empty); //render empty ones.
            else
                writer.WriteValue(value.ToString());
            writer.WriteEndElement();
        }
    }
    #endregion
}
[XmlRoot(“字典”)]
公共类SerializableDictionary:Dictionary,IXmlSerializable
{
#区域IXmlSerializable成员
public System.Xml.Schema.XmlSchema GetSchema()
{
返回null;
}
public void ReadXml(System.Xml.XmlReader)
{
//为了简洁起见,不包括在内
}
public void WriteXml(System.Xml.XmlWriter)
{
XmlSerializer keySerializer=新的XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer=新的XmlSerializer(typeof(TValue));
foreach(此.Keys中的TKey键)
{
writer.writeStart元素(key.ToString());
t值=此[键];
如果(值==null)
writer.WriteValue(String.Empty);//呈现空值。
其他的
WriteValue(value.ToString());
writer.writeedelement();
}
}
#端区
}

Attributes是字典键值对的名称。如果没有,您将无法将其反序列化回字典

如果不需要
元素,则需要将
WriteXml
方法移动到
产品
-类

public void WriteXml(XmlWriter writer)
{
    writer.WriteElementString("Identifier", Identifier.ToString("d"));
    XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
    foreach (TKey key in Attributes.Keys)
    {
        writer.WriteStartElement(key.ToString());
        TValue value = Attributes[key];
        if (value == null)
            writer.WriteValue(String.Empty); //render empty ones.
        else
            writer.WriteValue(value.ToString());
        writer.WriteEndElement();
    }
}
我不在乎:)有可能把它取下来吗?由于我可以控制WriteXml,所以我希望能够完全控制渲染。。。