创建数组作为单个节点c#

时间:2015-03-25 09:26:32

标签: c# xml

我想创建在序列化为xml之后的对象应该是:

<List>
        <Map>
            <Entry Key="1" Value="ASD" />
        </Map>
        <Map>
            <Entry Key="2" Value="DFE" />
        </Map>  
</List> 

而不是我的结果是:

<List>
        <Map>
            <Entry Key="1" Value="ASD" />
            <Entry Key="2" Value="DFE" />
        </Map>
</List>

我的代码:

 public partial class List {      
        private Map[] mapField;

        [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        [System.Xml.Serialization.XmlArrayItemAttribute("Entry", typeof(Map), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
        public Map[] Map {
            get {
                return this.mapField;
            }
            set {
                this.mapField = value;
            }
        }
 public partial class MapTypeEntry
 {     
        private string keyField;      
        private string valueField;

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Key {
            get {
                return this.keyField;
            }
            set {
                this.keyField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Value {
            get {
                return this.valueField;
            }
            set {
                this.valueField = value;
            }
        }
    }       

我做错了什么? 我想我在某个地方做错字,但我找不到哪里。
也许xml属性有问题? 可能这不应该是xmlArray项目吗?

编辑:

完整代码:

var mapEntry = new Map();
mapEntry.Key = "1";
mapEntry.Value = "ASD";
mapEntries.Add(mapEntry);

mapEntry = new Map();
mapEntry.Key = "2";
mapEntry.Value = "DFE";
mapEntries.Add(mapEntry);

var exampleType = new List();
List.Map = mapEntries.ToArray();

2 个答案:

答案 0 :(得分:2)

你需要改变我在下面实现的模型(只是简单的例子)。

namespace TestApp
{
    using System;
    using System.IO;
    using System.Xml.Schema;
    using System.Xml.Serialization;

    class Program
    {
        static void Main(string[] args)
        {
            var list = new List
            {
                Map = new[]
                {
                    new Entry {EntryItem = new EntryItem {Key = "1", Value = "ASD"}},
                    new Entry {EntryItem = new EntryItem {Key = "2", Value = "DFE"}}
                }
            };

            Console.Write(Serialize(list));
            Console.ReadKey();
        }

        private static string Serialize(List list)
        {
            var xmlSerializer = new XmlSerializer(typeof (List));
            var stringWriter = new StringWriter();
            xmlSerializer.Serialize(stringWriter, list);
            return stringWriter.ToString();
        }
    }

    [XmlRoot(ElementName = "Root")]
    public partial class List
    {
        [XmlArray(ElementName = "List")]
        [XmlArrayItem("Map", typeof (Entry), Form = XmlSchemaForm.Unqualified, IsNullable = false)]
        public Entry[] Map { get; set; }
    }

    public class Entry
    {
        [XmlElement("Entry")]
        public EntryItem EntryItem { get; set; }
    }

    public class EntryItem
    {
        [XmlAttribute]
        public string Key { get; set; }

        [XmlAttribute]
        public string Value { get; set; }
    }
}

所以它创建了XML:

<?xml version="1.0" encoding="utf-16"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <List>
    <Map>
      <Entry Key="1" Value="ASD" />
    </Map>
    <Map>
      <Entry Key="2" Value="DFE" />
    </Map>
  </List>
</Root>

答案 1 :(得分:1)

这是您的班级列表应该看的内容:

public partial class List
{
    private Map[] mapField;

    [XmlElement("Map", typeof(Map), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
    public Map[] Map
    {
        get
        {
            return this.mapField;
        }
        set
        {
            this.mapField = value;
        }
    }
[...]
}

对于你的Map类

public  class Map
{
   [XmlElement("Entry")]
   public KVPair Item { get; set; }
}

我称之为KVPair的那个

public class KVPair
{
    [XmlAttribute()]
    public string Key { get; set; }

    [XmlAttribute()]
    public string Value { get; set; }

}

Xml Produced是:

<?xml version="1.0"?>
<List xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Map>
    <Entry Key="1" Value="ASD" />
  </Map>
  <Map>
    <Entry Key="2" Value="DFE" />
  </Map>
</List>

您应该避免使用经常使用的类名,例如List。如果要使用其他名称调用它,请使用XmlRootAttribute来保持&#34; List&#34;对于你的xml文件。