【问题标题】:Deserializing XML response from Sharepoint 2007 Web services into Objects将来自 Sharepoint 2007 Web 服务的 XML 响应反序列化为对象
【发布时间】:2016-11-04 17:00:38
【问题描述】:

所以目前我设法得到一个包含所有列表项的 XML 响应:

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

当我检查输出时,它工作正常(尽管我认为我最终会想要某种过滤器以避免最终返回所有结果)。

读取为 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 服务项与模型对象之间的映射?

我最终也需要发布数据,所以这会很有趣...

【问题讨论】:

  • 取决于您使用的网络服务类型。例如 WCF 提供 .wsdl 文件,Visual Studio 可以生成 wsdl 文件中提供的所有类
  • 我正在使用 Sharepoint 2007 的通用 Web 服务访问:msdn.microsoft.com/en-us/library/office/…

标签: c# xml web-services sharepoint


【解决方案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 的属性有些疑问。怕0020的部分在反序列化过程中会导致问题:

避免在属性名称中包含数字字符

【讨论】:

  • 关于命名方案:SP2007 Web Services中XML响应内容的标准
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-10
  • 2014-09-23
  • 2011-09-14
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多