如何使用属性序列化字符串值列表

时间:2014-04-14 14:06:58

标签: c# xml serialization xml-serialization

我有问题。如何序列化具有属性的字符串条目列表?

<xml>
    <metadata>
        <entry key="key1">string1</entry>
        <entry key="key2">string2</entry>
        <entry key="key3">string3</entry>
    </metadata>
</xml>

我知道如何在没有属性的情况下做到这一点,但我不知道如何在我的情况下做同样的事情:

[Serializable]
[XmlRoot(ElementName = "xml")]
public class MyXml
{
    [XmlArray(ElementName = "metadata")]
    [XmlArrayItem(ElementName = "entry")]
    public List<string> Metadata { get; set; }
}

4 个答案:

答案 0 :(得分:1)

类似于此的东西可能适用。

class Entry{
   [XmlAttribute("key")]
   public string key {get;set;}
   [XmlText]
   public string entry{get;set;}
}

[Serializable]
[XmlRoot(ElementName = "xml")]
public class MyXml
{
   [XmlArray(ElementName = "metadata")]
   [XmlArrayItem(ElementName = "entry")]
   public List<Entry> Metadata { get; set; }
}

答案 1 :(得分:1)

您需要引入一个类来表示entry,这样您就可以提取key属性和值

public class Entry
{
    [XmlAttribute("key")]
    public string Key { get; set; }
    [XmlText]
    public string Value { get; set; }
}

[XmlRoot(ElementName="xml")]
public class MyXml
{
    [XmlArray("metadata")]
    [XmlArrayItem("entry")]
    public List<Entry> Metadata { get; set; }
}

答案 2 :(得分:1)

您需要创建一个单独的类来保存XmlAttributeXmlText

public class Entry
{
    [XmlAttribute("key")]
    public string Key { get; set; }
    [XmlText]
    public string Value { get; set; }
}

[Serializable]
[XmlRoot(ElementName = "xml")]
public class MyXml
{
    [XmlArray(ElementName = "metadata")]
    [XmlArrayItem(ElementName = "entry")]
    public List<Entry> Metadata { get; set; }
}

然后您可以使用您选择的序列化程序对其进行序列化。

var item = new MyXml
{
    Metadata = new List<Entry>
    {
        new Entry { Key = "key1", Value = "entry1" },
        new Entry { Key = "key2", Value = "entry2" },
        new Entry { Key = "key3", Value = "entry3" }
    }
};

var serializer = new XmlSerializer(typeof(MyXml));

string xml;

using(var stream = new StringWriter())
using(var writer = XmlWriter.Create(stream,
                                    new XmlWriterSettings { Indent = true }))
{
    serializer.Serialize(writer, item);
    xml = stream.ToString();
}

Console.WriteLine(xml);

结果:

<?xml version="1.0" encoding="utf-16"?>
<xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <metadata>
    <entry key="key1">entry1</entry>
    <entry key="key2">entry2</entry>
    <entry key="key3">entry3</entry>
  </metadata>
</xml>

答案 3 :(得分:0)

您的Metadata列表应定义一个名为Entry的新类型,该类型应如下所示

  [Serializable]

   public   class Entry
    {
       [XmlAttribute]
        public string Key { get; set; }
        [XmlText]
       public string value { get; set; }
    }

这里是你的主要课程

class Program
    {
        static void Main(string[] args)
        {

            MyXml  xml  = new MyXml();
            xml.Metadata.Add( new Entry(){Key = "test","content"});
        }


    }
    [Serializable]
    [XmlRoot(ElementName = "xml")]
    public class MyXml
    {
        [XmlArray(ElementName = "metadata")]
        [XmlArrayItem(ElementName = "entry")]
        public List<Entry> Metadata { get; set; }
    }