自定义.net xml字典(键,值)序列化

时间:2012-03-15 09:31:42

标签: .net xml serialization dictionary

我有这些课程:

Class ValueSet
{
List<Value> Values { get; set; }
}

Class Value
{
String Name { get; set;}
String Value { get; set;}
}

我想像这样序列化它们:

<Values>
<name1>value1</name1>
<name2>value2</name2>
<name3>value3</name3>
</Values>

这可能吗? 谢谢!

3 个答案:

答案 0 :(得分:1)

这个小方法让事情变得更容易:

public static XmlElement SerializeElement(XmlDocument doc, String nodeName, String nodeValue)
            {
                XmlElement newElement = doc.CreateElement(nodeName);

                if (nodeValue == null)
                {
                    nodeValue = String.Empty;
                }

                newElement.InnerXml = nodeValue;
                return newElement;
            }

然后你可以使用XmlDocument:

public void SaveToXml(Value _Val) //Make it your collection of Value(s) instead
{
   XmlDocument doc = new XmlDocument();

   //Create or overwrite the doc.
   File.Create(XmlFilePath).Close();

   XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
   doc.AppendChild(dec);

   XmlElement rootElem = Tools.SerializeElement(doc, "Root", null); //Root can be replaced with Values

   rootElem.AppendChild(SerializeElement(doc, _Val.Name, _Val.Value); //Put this in a loop if you want more than one.
   //Repeat the previous step to add any other properties within Root.

   //Save XML document
   doc.AppendChild(rootElem);
   doc.Save(AutoLoginXmlPath);
}

答案 1 :(得分:0)

回答我自己的问题..

我选择了这个:

public class CustomXMLKeyValueList : IXmlSerializable
{
    public List<CustomXMLKeyValueElement> Elements { get; set; }

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

    public void ReadXml(XmlReader reader)
    {
        //ToDo-experiment with this..
        throw new NotImplementedException("This document can not be deserialized.");
    }

    public void WriteXml(XmlWriter writer)
    {
        foreach (var element in this.Elements)
        {
            element.WriteXml(writer);
        }
    }
}

public class CustomXMLKeyValueElement : IXmlSerializable
{
    public string Key { get; set; }
    public string Value { get; set; }

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

    public void ReadXml(XmlReader reader)
    {
        //ToDo-experiment with this..
        throw new NotImplementedException("This document can not be deserialized.");
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteStartElement(this.Key);
        writer.WriteString(this.Value);
        writer.WriteEndElement();
    }
}


public class KeyValueCustomXMLSerialization_Fixture
{
    public void Test()
    {
        var kvl = new CustomXMLKeyValueList() { Elements = new List<CustomXMLKeyValueElement>() };

        kvl.Elements.Add(new CustomXMLKeyValueElement() { Key = "key1", Value = "value1" });
        kvl.Elements.Add(new CustomXMLKeyValueElement() { Key = "key2", Value = "value2" });
        kvl.Elements.Add(new CustomXMLKeyValueElement() { Key = "key3", Value = "value3" });

        var xml = Serializer.SerializeToXMLString(kvl, true, true);
    }
}

这不是最好的解决方案,但最后,xml var包含以下漂亮的字符串:

<CustomXMLKeyValueList>
  <key1>value1</key1>
  <key2>value2</key2>
  <key3>value3</key3>
</CustomXMLKeyValueList>

'Serializer'是一个神奇的类,它执行类似this的操作,而(因为'true,true')省略了名称空间和xml声明..

答案 2 :(得分:0)

public static class Serializer
{


    public static string SerializeToXMLString<T>(T obj, bool omitXmlDeclaration = false, bool omitNamespaces = false)
        where T : class
    {
        return SerializeToXMLString(obj, omitXmlDeclaration, GetNamespaces(omitNamespaces));
    }

    public static string SerializeToXMLString<T>(T obj, bool omitXmlDeclaration = false, params XmlSerializerNamespaces[] ns)
        where T : class
    {
        var serializer = new XmlSerializer(typeof(T));
        var memstream = new MemoryStream();

        var writer = GetXmlWriter(memstream, GetXmlWriterSettings(omitXmlDeclaration));

        if (ns != null && ns.Length > 0)
            serializer.Serialize(writer, obj, ns[0]);
        else
            serializer.Serialize(writer, obj);

        memstream.Seek(0, SeekOrigin.Begin);
        var sr = new StreamReader(memstream);

        var str = sr.ReadToEnd();

        writer.Flush();
        writer.Close();

        sr.Dispose();
        memstream.Dispose();

        return str;
    }

    private static XmlWriterSettings GetXmlWriterSettings(bool omitDeclaration)
    {
        var settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = omitDeclaration;

        return settings;
    }

    private static XmlSerializerNamespaces[] GetNamespaces(bool omit)
    {
        var nspaces = new List<XmlSerializerNamespaces>();

        if (omit)
        {
            var ns = new XmlSerializerNamespaces();
            ns.Add(String.Empty, String.Empty);

            nspaces.Add(ns);
        }

        return nspaces.ToArray();
    }
}