String List Serializer

时间:2013-01-02 15:09:05

标签: c# xml-serialization

我有一个必须序列化的表。

我的班级,我向其添加表格值

  public class NewCVXml
{
    [XmlArray]
    [XmlArrayItem(ElementName = "Name")]
    public List<string> FieldFirst { get; set; }
    [XmlArray]
    [XmlArrayItem(ElementName = "Value")]
    public List<string> FieldSecond { get; set; }
    [XmlArray]
    [XmlArrayItem(ElementName = "State")]
    public List<string> FieldThird { get; set; }

}

这是来自ajax(Request.Form)

的价值
  

{FIELD1 = NAME1&安培; field1的= NAME2&安培; field1的= NAME3&安培; FIELD2 =值1&安培; FIELD2 =值2&安培; FIELD2 =值3&安培;字段3 =私有&安培;字段3 =公共&安培;字段3 =公共}

这是我的c#函数,由ajax调用(用于发布)

       string xmlText;
        NewCVXml newCv = new NewCVXml();
        newCv.FieldFirst =  Request.Form.GetValues(0).ToList();
        newCv.FieldSecond = Request.Form.GetValues(1).ToList();
        newCv.FieldThird = Request.Form.GetValues(2).ToList();

        try
        {
            XmlSerializer serializer = new XmlSerializer(newCv.GetType());
            MemoryStream ms = new MemoryStream();
            using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8) { Formatting = Formatting.Indented })
            {

                serializer.Serialize(writer, newCv);
                ms = (MemoryStream)writer.BaseStream;
                xmlText = new UTF8Encoding().GetString(ms.ToArray());
                ms.Dispose();
                return Content(xmlText);
            }
        }
        catch (Exception e)
        {

            return Content( "Hata: " + e.Message);

        }

并且此xml创建

    <?xml version="1.0" encoding="utf-8"?>
<NewCVXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FieldFirst>
    <Name>name1</Name>
    <Name>name2</Name>
    <Name>name3</Name>
  </FieldFirst>
  <FieldSecond>
    <Value>value1</Value>
    <Value>value2</Value>
    <Value>value3</Value>
  </FieldSecond>
  <FieldThird>
    <State>Private</State>
    <State>Public</State>
    <State>Public</State>
  </FieldThird>
</NewCVXml>

my table

但我想要像xml那样的东西

<?xml version="1.0" encoding="utf-8"?>
<NewCVXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Rows>
    <name1 state="Private">value1</name1>
    <name2 state="Public">value2</name2>
    <name3 state="Public">value3</name3>
</Rows>
</NewCVXml>

我不知道如何创建像xml那样。我不能为每一行使用一个类。因为行的单元格是可变的。我的意思是,它可以是name1或XXX或YYY或......它取决于用户。一切都是动态的,我认为我需要使用字符串列表并从列表值创建标记名。

2 个答案:

答案 0 :(得分:1)

您的类结构不会按照您希望使用标准XmlSerializer的方式序列化为XML。相反,您可以使用IXmlSerializable实现自定义序列化。

http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

public class NewCVXml : IXmlSerializable {

    public List<string> FieldFirst { get; set; }
    public List<string> FieldSecond { get; set; }
    public List<string> FieldThird { get; set; }

    public void WriteXml (XmlWriter writer)
    {
        // Custom Serialization Here
    }

    public void ReadXml (XmlReader reader)
    {
        // Custom Deserialization Here
    }

    public XmlSchema GetSchema()
    {
        return(null);
    }

}

答案 1 :(得分:0)

罗伯特格雷夫斯说,我需要一个自定义的xml序列化程序。我使用了Robert给出并编辑的代码。它现在有效,也许有些人需要它然后我把它放在这里

using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace E_Cv.Functions
{
    public class NewCVXml :IXmlSerializable
    {

        public List<string> FieldFirst { get; set; }
        public List<string> FieldSecond { get; set; }
        public List<string> FieldThird { get; set; }

        public void WriteXml(XmlWriter writer)
        {


            for (int i = 0; i < FieldFirst.Count; i++)
            {

                writer.WriteStartElement("Satir");

                writer.WriteAttributeString("AlanIsmi",FieldFirst[i]);
                //writer.WriteString(FieldFirst[i]);    
                writer.WriteAttributeString("AlanTuru", FieldThird[i]);
                writer.WriteString(FieldSecond[i]); 
                writer.WriteEndElement();
            }


        }

        public void ReadXml(XmlReader reader)
        {
            // Custom Deserialization Here
        }

        public XmlSchema GetSchema()
        {
            return (null);
        }
    }
}