C# Xml序列化嵌套类
很抱歉,我检查了所有的例子,但没有得到任何帮助。 我的xml结构应该是C# Xml序列化嵌套类,c#,xml,xml-serialization,C#,Xml,Xml Serialization,很抱歉,我检查了所有的例子,但没有得到任何帮助。 我的xml结构应该是 <?xml version="1.0"?> <Project ModelVersion="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Models> <Model> <Id>9
<?xml version="1.0"?>
<Project ModelVersion="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Models>
<Model>
<Id>987214</Id>
<prop1></prop1>
<prop2></prop2>
<Sections>
<Section>
<id>3548A</id>
<prop1>true</prop1>
<BaseSection xsi:type="Multiple">
<prop1>Ijk</prop1>
<prop2>Lmn</prop2>
</BaseSection>
</Section>
<Section>
<id>3548B</id>
<prop1>true</prop1>
<BaseSection xsi:type="Single">
<prop1>Xyz</prop1>
<prop2>Abc</prop2>
</BaseSection>
</Section>
</Sections>
</Model>
</Models>
</Project>
这是我的专题课
Project settings = null;
Stream stream = File.Open(folderPath + filename, FileMode.Open);
XmlSerializer xs = new XmlSerializer(typeof(Project));
settings = (Project)xs.Deserialize(stream);
stream.Close();
现在我的问题是,在执行这部分代码之后,设置确实包含“Models”,但不包含模型的细节。模型是模型列表,两者都标记为[Serilizable()],节是节列表,两者也标记为[Serilizable()]
我花了一些时间在这上面,因为我认为这几乎不需要30分钟的工作,但没有运气
任何帮助都将不胜感激
提前感谢在您的XML中,
模型
是模型
的数组,但这并没有正确地反映在您的项目
类中。你需要改变
[XmlElement("Model")]
public Model Model { get; set; }
到
我觉得自己很愚蠢,那就行了,它确实起了作用:)。谢谢你用橙色供电!!我很感激,没问题。这些小错误总是发生。
[XmlElement("Model")]
public Model Model { get; set; }
[XmlArray("Models")]
public List<Model> Models { get; set; }
[Serializable]
public class Model
{
[XmlElement("Id")]
public int Id { get; set; }
[XmlArray("Sections")]
public List<Section> Sections { get; set; }
}