我正在尝试从Zoho API检索数据,然后我想反序列化对象中的返回数据。
XML结构:
<response uri="/crm/private/xml/Potentials/getSearchRecords">
<result>
<Potentials>
<row no="1">
<FL val="POTENTIALID">0000000000000</FL>
<FL val="SMOWNERID">000000000000</FL>
.......
</row>
<row no="2">
.......
</row>
</Potentials>
</result>
</response>
在这里我尝试了但没有成功。
[Serializable()]
public class ZohoXML
{
public ZohoXML() { }
[XmlElement("POTENTIALID")]
public string POTENTIALID { get; set; }
........
}
[XmlRoot("response")]
public class Response
{
[XmlElement("result")]
public Result[] ResultList { get; set; }
}
public class Result
{
[XmlArray("Potentials"), XmlArrayItem("row")]
public Row[] RowList { get; set; }
}
[XmlRoot("row")]
public class Row
{
[XmlArray("Potentials")]
[XmlArrayItem("row", typeof(ZohoXML))]
public ZohoXML[] ZohoXML { get; set; }
}
这就是我反序列化的方式:
Response ZohoXMLCollection = null;
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Response));
StreamReader reader = new StreamReader(strFile);
ZohoXMLCollection = (Response)serializer.Deserialize(reader);
reader.Close();
有人可以帮助您找出代码中的问题吗?
答案 0 :(得分:2)
如果您使用的是较新版本的VS,则可以在编辑菜单中使用Edit > Paste Special > Paste XML as Classes
。假设您的XML结构正确,这将为您提供:
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class response
{
private responseResult resultField;
private string uriField;
/// <remarks/>
public responseResult result
{
get
{
return this.resultField;
}
set
{
this.resultField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string uri
{
get
{
return this.uriField;
}
set
{
this.uriField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class responseResult
{
private responseResultRow[] potentialsField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("row", IsNullable = false)]
public responseResultRow[] Potentials
{
get
{
return this.potentialsField;
}
set
{
this.potentialsField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class responseResultRow
{
private responseResultRowFL[] flField;
private byte noField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("FL")]
public responseResultRowFL[] FL
{
get
{
return this.flField;
}
set
{
this.flField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte no
{
get
{
return this.noField;
}
set
{
this.noField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class responseResultRowFL
{
private string valField;
private byte valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string val
{
get
{
return this.valField;
}
set
{
this.valField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public byte Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
您需要清理名称,但现在这很好。
然后你的反序列化将如下所示:
XmlSerializer serializer = new XmlSerializer(typeof(response));
response res = new response();
using (StreamReader reader = new StreamReader(fileLocation))
{
res = serializer.Deserialize(reader) as response;
}
请注意,StreamReader
实现IDisposable
接口,因此应在using
块中使用,以确保自动分配资源。