我有一个必须序列化的表。
它是我的课,我向它添加表值
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 或......它取决于用户。一切都是动态的,然后我认为我需要使用字符串列表并从列表值创建标记名。