【问题标题】:How to serialize a list of string values with attributes如何序列化具有属性的字符串值列表
【发布时间】:2014-04-14 14:06:57
【问题描述】:

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

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

【问题讨论】:

    标签: c# xml serialization xml-serialization


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

      类似的东西可能适用。

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

      【讨论】:

        【解决方案3】:

        您需要引入一个类来表示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; }
        }
        

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-10-23
            • 2017-04-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多