C# 这种xml序列化有什么问题?
我正在从事一个项目,该项目几乎需要一个xml文件,并用C#对其进行序列化,以便可以使用它格式化word文档。到目前为止,一切都进展顺利,它正在解析数千个xml标记,到目前为止,创建了一个86页的docco 然而,在文档完成之前,我需要完成最后两个标记,由于某种原因,序列化无法在其中一个上工作C# 这种xml序列化有什么问题?,c#,xml,debugging,serialization,xml-serialization,C#,Xml,Debugging,Serialization,Xml Serialization,我正在从事一个项目,该项目几乎需要一个xml文件,并用C#对其进行序列化,以便可以使用它格式化word文档。到目前为止,一切都进展顺利,它正在解析数千个xml标记,到目前为止,创建了一个86页的docco 然而,在文档完成之前,我需要完成最后两个标记,由于某种原因,序列化无法在其中一个上工作 - <layout_row> - <layout_cell type="attribute"> <attribute_and_presentation a
- <layout_row>
- <layout_cell type="attribute">
<attribute_and_presentation attribute="Name" />
<layout_group is_column="true" label_column_width="100" />
</layout_cell>
</layout_row>
问题在于layout_组,出于某种原因,它根本不会序列化。我已经做了几个小时了,我觉得我肯定错过了一些很明显的东西,但就我的一生而言,我就是无法解决它
值得注意的是,在这个类中,类型和属性以及表示都可以很好地序列化。您说获取一个xml文件并将其序列化,但我认为您的意思是反序列化。无论如何,这里有一个使用您发布的类的双向工作示例 虽然我同意其他意见,即您的应该使用属性,但我不认为这是您的问题。我的示例使用字段(主要是您自己的代码)。事实上,这些国家表示: 可以序列化的项
- 公共类的公共读/写属性和字段
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Xml.Serialization;
命名空间控制台应用程序1
{
班级计划
{
静态字符串xml_to_反序列化=@”
";
静态void Main(字符串[]参数)
{
var serializer=new System.Xml.Serialization.XmlSerializer(typeof(layout_cell));
//试验脱硅
使用(var-stringReader=new-stringReader(xml到反序列化))
使用(var reader=System.Xml.XmlReader.Create(stringReader))
{
var result=序列化程序。反序列化(读取器);
result.ToString();//此处的断点指向examimne
}
//测试序列化
var toSerialize=新布局\单元()
{
type=“some type”,
属性_和_表示=新属性_和_表示()
{
attribute=“某些属性”
},
布局组=新布局组()
{
是_column=“true”,
label\u column\u width=“100”
}
};
使用(var writer=new System.IO.StringWriter())
{
serializer.Serialize(writer,toSerialize);
Console.WriteLine(writer.ToString());
}
控制台.WriteLine(“完成,点击回车”);
Console.ReadLine();
}
}
[System.Serializable()]
公共类布局单元
{
[XmlAttribute(“类型”)]
公共字符串类型;
[XmlElement(“属性和表示”)]
公共属性和表示属性和表示;
[XmlElement(“布局组”)]
公共布局组布局组;
}
[System.Serializable()]公共类属性和表示
{
[XmlAttribute]
公共字符串属性;
}
[System.Serializable()]公共类布局\u组
{
[XmlAttribute(“is_列”)]公共字符串是_列;
[XmlAttribute(“label\u column\u width”)]公共字符串label\u column\u width;
}
}
我假设您在layout\u组中实际获得了一些数据
?如果没有,默认情况下它不会序列化。您的属性_和_表示
和布局_组
类的定义是什么?[System.Serializable()]公共类属性_和_表示{[XmlAttribute]公共字符串属性;}和[System.Serializable()]公共类布局_组{[XmlAttribute](“is_column”)]公共字符串is_column;[XmlAttribute(“label_column_width”)]公共字符串label_column_width;}我的天啊,代码的回复并不能很好地工作。无论如何,这两个类都只是声明元素中提供的属性。属性和表示像一个符咒,布局组则不然@SimonWhitehead@Jessixia最好是在你的问题中添加代码,而不是因为这个原因而添加注释。是的,对不起,关于d,你是对的eserialize,我总是把这些混淆起来。嗯,这很不寻常,因为你的反序列化代码看起来和我的一样…我想这意味着问题源于整个XML文件…这对我来说没有意义,如果它到达XML文件中的元素,那么该元素的子集的行为是否与出现在较大集合的一部分?当遇到这些奇怪的问题时,我开始切掉文件的一部分,并试图得到产生错误的最小集合。我想这将是唯一的实际选择。至于属性和字段,两者都做相同的事。我想我只需要回到绘图板上。非常令人沮丧的是,不是hough-uu-已经相当成功地反序列化了20个独特的元素类及其属性,而这一个元素只给了我问题=/无论如何,谢谢你的帮助:)所以这有点尴尬>\uu<但是,我在word文档中一直看到的一个部分似乎是布局组不用于列的部分o、 它有点起作用了
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;
}