我正在开发一个项目,它几乎需要一个xml文件并在C#中序列化它,以便它可以用来格式化word文档。到目前为止,一切都很顺利,它解析了几千个xml标签,到目前为止,它非常开心地创建了一个86页的docco。
但是,我正在完成文档完成之前需要完成的最后两个标记,并且出于某种原因,序列化只是对其中一个标记不起作用。
- <layout_row>
- <layout_cell type="attribute">
<attribute_and_presentation attribute="Name" />
<layout_group is_column="true" label_column_width="100" />
</layout_cell>
</layout_row>
上面是我正在序列化的xml代码示例
using System.Collections;
using System.Xml.Serialization;
using System.Xml.Xsl;
using System.Xml.Schema;
using System.Runtime.Serialization;
using System.Collections.Generic;
[System.Serializable()]
public class layout_cell
{
[XmlAttribute("type")]
public string type;
[XmlElement("attribute_and_presentation")]
public attribute_and_presentation attribute_and_presentation;
[XmlElement("layout_group")]
public layout_group layout_group;
}
[System.Serializable()]
public class attribute_and_presentation {
[XmlAttribute]
public string attribute;
}
[System.Serializable()]
public class layout_group {
[XmlAttribute("is_column")]
public string is_column;
[XmlAttribute("label_column_width")]
public string label_column_width;
}
问题在于layout_group,由于某种原因它根本就不会序列化。我已经在这里工作了几个小时,我觉得我必须错过一些非常明显的东西,但对于我的生活,我无法解决这个问题。
值得注意的是,type和attribute_and_presentation在这个类中完全序列化。
答案 0 :(得分:1)
你说需要一个xml文件并序列化它,但我认为你的意思是反序列化。无论如何,这是使用您发布的类的两个方向的工作示例。
虽然我同意其他评论应该使用属性,但我认为这不是您的问题。我的例子使用字段(它主要是你自己的代码)。实际上Documentation表示可以使用:
可以序列化的项目
- 公共读/写属性和公共类的字段。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
static string xml_to_deserialize = @"
<layout_cell type=""attribute"">
<attribute_and_presentation attribute=""Name"" />
<layout_group is_column=""true"" label_column_width=""100"" />
</layout_cell>
";
static void Main(string[] args)
{
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(layout_cell));
//test desieralization
using (var stringReader = new StringReader(xml_to_deserialize))
using (var reader = System.Xml.XmlReader.Create(stringReader))
{
var result = serializer.Deserialize(reader);
result.ToString(); //breakpoint here to examimne
}
//test serialization
var toSerialize = new layout_cell()
{
type = "some type",
attribute_and_presentation = new attribute_and_presentation()
{
attribute = "some attribute"
},
layout_group = new layout_group()
{
is_column = "true",
label_column_width = "100"
}
};
using (var writer = new System.IO.StringWriter())
{
serializer.Serialize(writer, toSerialize);
Console.WriteLine(writer.ToString());
}
Console.WriteLine("done, hit enter.");
Console.ReadLine();
}
}
[System.Serializable()]
public class layout_cell
{
[XmlAttribute("type")]
public string type;
[XmlElement("attribute_and_presentation")]
public attribute_and_presentation attribute_and_presentation;
[XmlElement("layout_group")]
public layout_group layout_group;
}
[System.Serializable()] public class attribute_and_presentation
{
[XmlAttribute]
public string attribute;
}
[System.Serializable()] public class layout_group
{
[XmlAttribute("is_column")] public string is_column;
[XmlAttribute("label_column_width")] public string label_column_width;
}
}