2014-04-14 37 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; } 
    } 
相关问题