【发布时间】:2019-10-03 06:44:55
【问题描述】:
我正在将我的字符串反序列化到我的类中。 问题是嵌套子元素的一部分或其属性返回 null。
这是我的反序列化函数:
public static T DeSerialize<T>(this string xml)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (TextReader reader = new StringReader(xml))
{
T result = (T)serializer.Deserialize(reader);
return result;
}
}
这是我的课程:
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.specifiedcompany.com/API")]
public partial class VideoGames
{
private GameType[] typesField;
private Platform platformField;
private string memoField;
[System.Xml.Serialization.XmlArray("Types", Order = 0)]
[System.Xml.Serialization.XmlArrayItem("Data", IsNullable = false)]
public GameType[] Types
{
get
{
return this.typesField;
}
set
{
this.typesField= value;
}
}
[System.Xml.Serialization.XmlElementAttribute("Platform", Order = 1)]
public Platform Platform
{
get
{
return this.platformField;
}
set
{
this.platformField= value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Memo
{
get
{
return this.memoField;
}
set
{
this.memoField= value;
}
}
}
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.specifiedcompany.com/API")]
public partial class Platform
{
private decimal compressedField;
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal Compressed
{
get
{
return this.compressedField;
}
set
{
this.compressedField= value;
}
}
}
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.specifiedcompany.com/API")]
public partial class GameType
{
private Code[] codeField;
private string nameField;
[System.Xml.Serialization.XmlArrayAttribute("Code", Order = 0)]
[System.Xml.Serialization.XmlArrayItemAttribute("Category", IsNullable = false)]
public Code[] Code
{
get
{
return this.codeField;
}
set
{
this.codeField= value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField= value;
}
}
}
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.specifiedcompany.com/API")]
public partial class Code
{
private string textField;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Text
{
get
{
return this.textField;
}
set
{
this.textField= value;
}
}
}
这里是xml:
<VideoGames Memo="1">
<Types>
<Data Name="RPG">
<Code>
<Category Text="TestingData"/>
</Code>
</Data>
</Types>
<Platform Compressed="3.2876"/>
</VideoGames>
我正在使用这样的反序列化:
string xml = "";//xml string above.
VideoGames a = xml.DeSerialize<VideoGames>();
我希望类中的每个属性都有值,但只有 VideoGames 中的 Memo 有值,其他的都是 null。
例如:a.Types 为空,但a.Memo 为“1”。
我已经尝试了该主题搜索的所有相关问题,但没有一个有效。
注意:
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xml);
我检查了这个 xDoc,它加载完美,没有任何丢失。
【问题讨论】:
-
会不会是
Types和Platform没有初始化? -
@bolkay我试过了,
this.typesField = new GameType[] { };,然后我得到了一个空数组,不是空的。 -
我怀疑这是初始化数组的有效方法。例如,您可以设置
typesField[5]=something?吗?在这种情况下,根据您的初始化方法{},您的数组中没有任何内容。 -
哦,我知道这里出了什么问题。我刚刚尝试了
this.typesField = new GameType[] { new GameType() };,我得到了一个包含 1 个元素的 GameType 数组,一个没有任何值的新 GameType 类。为什么反序列化不能正确执行?我的其他班级没有这样的问题。 -
a.Platform也为空。看来现在只能反序列化top节点的属性了。
标签: c# xml deserialization xml-deserialization