我正在尝试阅读一个简单的xml并通过类反序列化它。但它返回xml中存在的所有参数的空值。 在反序列化以下xml时,任何人都可以帮我解决空的返回值问题。
<Batch>
<description>example</description>
<fileField>DesktopA</fileField>
<output>The</output>
<input>home</input>
<input>green</input>
<parameters>
<action>1</action>
<item>2</item>
</parameters>
<parameters>
<action>1</action>
<item>4</item>
</parametersField>
</Batch>
班级:
[Serializable()]
public partial class Batch {
private string description;
private string fileField;
private string output;
private string[] input;
private Parameter[] parameters;
/// <remarks/>
public string Description {
get {
return this.description;
}
set {
this.description = value;
}
}
/// <remarks/>
public string File {
get {
return this.fileField;
}
set {
this.fileField = value;
}
}
/// <remarks/>
public string Output {
get {
return this.output;
}
set {
this.output = value;
}
}
/// <remarks/>
[XmlElement]
public string[] Input {
get {
return this.input;
}
set {
this.input = value;
}
}
/// <remarks/>
[XmlElement]
public Parameter[] Parameters {
get {
return this.parameters;
}
set {
this.parameters = value;
}
}
}
/// <remarks/>
public partial class Parameter {
private sbyte action;
private object item;
/// <remarks/>
public sbyte Action {
get {
return this.action;
}
set {
this.action = value;
}
}
/// <remarks/>
[XmlElement]
public object Item {
get {
return this.item;
}
set {
this.item = value;
}
}
deserializaion方法:
XmlSerializer reader;
Type bt = batchInfo.GetType();
reader = new XmlSerializer(bt);
System.IO.StreamReader file =
new System.IO.StreamReader(testScriptFile, System.Text.Encoding.Default);
batchInfo = (Batch)reader.Deserialize(file);
答案 0 :(得分:0)
如果您正确检查了XML,则可以看到参数存在问题。你有,但它应该是
<Batch>
<description>example</description>
<fileField>DesktopA</fileField>
<output>The</output>
<input>home</input>
<input>green</input>
<parameters>
<action>1</action>
<item>2</item>
</parameters>
<parameters>
<action>1</action>
<item>4</item>
</parameters>
</Batch>
希望它有所帮助!
答案 1 :(得分:0)
您可以使用以下模型反序列化您的xml。它使用XmlElement
属性。
public class Parameter
{
[XmlElement("action")]
public int Action { get; set; }
[XmlElement("item")]
public int Item { get; set; }
}
[XmlRoot("Batch")]
public class Batch
{
[XmlElement("description")]
public string Description { get; set; }
[XmlElement("fileField")]
public string FileField { get; set; }
[XmlElement("output")]
public string Output { get; set; }
[XmlElement("input")]
public List<string> Inputs { get; set; }
[XmlElement("parameters")]
public List<Parameter> Parameters { get; set; }
}
通过使用此代码,您可以执行反序列化。
var xml = @"
<Batch>
<description>example</description>
<fileField>DesktopA</fileField>
<output>The</output>
<input>home</input>
<input>green</input>
<parameters>
<action>1</action>
<item>2</item>
</parameters>
<parameters>
<action>1</action>
<item>4</item>
</parameters>
</Batch>";
var serializer = new XmlSerializer(typeof(Batch));
var textReader = new StringReader(xml);
var obj = (Batch) serializer.Deserialize(textReader);
或者从文件中读取
var serializer = new XmlSerializer(typeof(Batch));
using (var fileStream = new FileStream(@"C:\mypath\myFile.xml", FileMode.Open))
{
var obj = (Batch)serializer.Deserialize(fileStream);
}