1

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

它是我的课,我向它添加表值

  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=value1&field2=value2&field2=value3&field3=Private&field3=Public&field3=Public}

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

       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>

我的桌子

但我想要类似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 或......它取决于用户。一切都是动态的,然后我认为我需要使用字符串列表并从列表值创建标记名。

4

2 回答 2

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

}
于 2013-01-02T15:17:42.243 回答
0

正如 Robert Graves 所说,我需要一个自定义的 xml 序列化程序。我使用了罗伯特给出的代码并对其进行了编辑。它现在有效,也许有些人会需要它然后我把它放在这里

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);
        }
    }
}
于 2013-01-02T16:43:24.900 回答