2014-04-14 39 views
2

我遇到問題。如何序列化具有屬性的字符串條目列表?如何序列化具有屬性的字符串值列表

<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; } 
} 

回答

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> 
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

您需要引入一個類來表示一個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; } 
} 
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; } 
    } 
相關問題