2016-11-04 122 views
0

所以目前我设法让所有的列表项作为一个XML响应:反序列化XML响应为对象

XmlNode nodeListItems = ListObject.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, null); 

,当我检查输出,它工作正常(though-我认为我最终会希望有某种过滤器,以避免最终返回所有结果)。

的数据是这样的,当作为XML数据读取:

<?xml version="1.0" encoding="UTF-8"?> 
<rs:data xmlns:rs="urn:schemas-microsoft-com:rowset" ItemCount="3"> 
    <z:row xmlns:z="#RowsetSchema" ows_h_id="123" ows_Status="Needs To Be Run" ows_Report_x0020_Type="Incremental Code Review" /> 

    <z:row xmlns:z="#RowsetSchema" ows_h_id="456" ows_Status="On Master" ows_Report_x0020_Type="Code Review" /> 

    <z:row xmlns:z="#RowsetSchema" ows_h_id="789" ows_Status="--" ows_Report_x0020_Type="Code Review" /> 
</rs:data> 

我如何反序列化这一个类模型,如:

public Class ItemList 
    { 
    public int Hid {get; set; } 
    public string Status {get; set; } 
    public string Type {get; set; } 
    } 

是否有管理之间的映射任何工具Web服务项目来建模对象?

我最终将需要太发布的数据,这样会很有趣......

+0

取决于你用什么样的网络服务。例如WCF提供.wsdl文件,Visual Studio可以生成wsdl文件中提供的所有类 – Fabio

+0

我正在使用Sharepoint 2007的通用Web服务访问:https://msdn.microsoft.com/en-us/library/office /bb862916(v=office.12).aspx – Bitz

回答

1

您可以使用XML序列化属性

[XmlRoot("data", Namespace = "urn:schemas-microsoft-com:rowset")] 
public class Data 
{ 
    [XmlElement("row", Namespace = "#RowsetSchema")] 
    public List<ItemList> Rows { get; set; } 
} 

public class ItemList 
{ 
    [XmlAttribute("ows_h_id")] 
    public int Hid {get; set; } 

    [XmlAttribute("ows_Status")] 
    public string Status {get; set; } 

    [XmlAttribute("ows_Report_x0020_Type")] 
    public string Type {get; set; } 
} 

然后用XmlSerializer的

var serializer = new XmlSerializer(typeof(Data)); 

var data = (Data)serializer.Deserialize(yourStreamReader); 

反序列化我对名字ows_Report_x0020_Type有一些疑问。 Affraid部分0020可以反序列化过程引起的问题:具有数字字符

避免在属性名

+0

至于命名机制:它在Web Services for SP2007中的XML响应内容标准 – Bitz