当多个元素具有相同名称时,如何使用c#和XDocument将XML解析为对象?

时间:2014-03-19 17:21:18

标签: c# xml sharepoint

我使用SharePoint 2013的REST API来解析XML,但是我在解析某个XML块时遇到了问题。这是我可能会得到的一些XML的截断示例,我没有解析问题:

<feed xml:base="http://server/DesktopApplications/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
  <id>2dff4586-6b7d-4186-ae63-4d048f74a112</id>
  <title />
  <updated>2014-03-19T15:32:15Z</updated>
  <entry m:etag=""19"">
    <id>http://server/DesktopApplications/_api/Web/Lists(guid'0ac46109-38d9-4070-96b7-f90085b56f1e')</id>
    <category term="SP.List" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <link rel="edit" href="Web/Lists(guid'0ac46109-38d9-4070-96b7-f90085b56f1e')" />
    <title />
    <updated>2014-03-19T15:32:15Z</updated>
    <author>
      <name />
    </author>
    <content type="application/xml">
      <m:properties>
        <d:BaseTemplate m:type="Edm.Int32">101</d:BaseTemplate>
        <d:BaseType m:type="Edm.Int32">1</d:BaseType>
        <d:Id m:type="Edm.Guid">0ac46109-38d9-4070-96b7-f90085b56f1e</d:Id>
        <d:ListItemEntityTypeFullName>SP.Data.Shared_x0020_DocumentsItem</d:ListItemEntityTypeFullName>
        <d:Title>Documents</d:Title>
      </m:properties>
    </content>
  </entry>
  ...
</feed>

我可以使用以下内容XDocument解析这个问题:

private static readonly XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
private static readonly XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";

...    

List<KLList> lists = doc.Descendants(m + "properties").Select(
    list => new KLList() 
    { 
        Id = list.Element(d + "Id").Value, 
        Title = list.Element(d + "Title").Value,
        ListItemEntityTypeFullName = list.Element(d + "ListItemEntityTypeFullName").Value,
        BaseType = (BaseType)Convert.ToInt32(list.Element(d + "BaseType").Value),
        ListTemplateType = (ListTemplateType)Convert.ToInt32(list.Element(d + "BaseTemplate").Value)
    }).ToList();

当我扩展我的查询以获取列表的根文件夹的服务器相对URL时,我得到这样的XML,我遇到了麻烦:

<feed xml:base="http://server/DesktopApplications/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
  <id>a01c22dc-a87f-4253-91d1-0a6ef8efa6d0</id>
  <title />
  <updated>2014-03-19T15:27:11Z</updated>
  <entry m:etag=""19"">
    <id>http://server/DesktopApplications/_api/Web/Lists(guid'0ac46109-38d9-4070-96b7-f90085b56f1e')</id>
    <category term="SP.List" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <link rel="edit" href="Web/Lists(guid'0ac46109-38d9-4070-96b7-f90085b56f1e')" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/RootFolder" type="application/atom+xml;type=entry" title="RootFolder" href="Web/Lists(guid'0ac46109-38d9-4070-96b7-f90085b56f1e')/RootFolder">
      <m:inline>
        <entry>
          <id>http://server/DesktopApplications/_api/Web/Lists(guid'0ac46109-38d9-4070-96b7-f90085b56f1e')/RootFolder</id>
          <category term="SP.Folder" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
          <link rel="edit" href="Web/Lists(guid'0ac46109-38d9-4070-96b7-f90085b56f1e')/RootFolder" />
          <title />
          <updated>2014-03-19T15:27:11Z</updated>
          <author>
            <name />
          </author>
          <content type="application/xml">
            <m:properties>
              <d:ServerRelativeUrl>/DesktopApplications/Shared Documents</d:ServerRelativeUrl>
            </m:properties>
          </content>
        </entry>
      </m:inline>
    </link>
    <title />
    <updated>2014-03-19T15:27:11Z</updated>
    <author>
      <name />
    </author>
    <content type="application/xml">
      <m:properties>
        <d:BaseTemplate m:type="Edm.Int32">101</d:BaseTemplate>
        <d:BaseType m:type="Edm.Int32">1</d:BaseType>
        <d:Id m:type="Edm.Guid">0ac46109-38d9-4070-96b7-f90085b56f1e</d:Id>
        <d:ListItemEntityTypeFullName>SP.Data.Shared_x0020_DocumentsItem</d:ListItemEntityTypeFullName>
        <d:Title>Documents</d:Title>
      </m:properties>
    </content>
  </entry>
  ...
</feed>

我尝试使用基本相同的代码,因为我希望ServerRelativeUrl位于同一个对象上:

List<KLList> lists = doc.Descendants(m + "properties").Select(
    list => new KLList() 
    { 
        Id = list.Element(d + "Id").Value, 
        Title = list.Element(d + "Title").Value,
        ListItemEntityTypeFullName = list.Element(d + "ListItemEntityTypeFullName").Value,
        BaseType = (BaseType)Convert.ToInt32(list.Element(d + "BaseType").Value),
        ListTemplateType = (ListTemplateType)Convert.ToInt32(list.Element(d + "BaseTemplate").Value),
        RelativeUrl = list.Element(d + "ServerRelativeUrl").Value
    }).ToList();

但这不再适用。 doc.Descendants(m + "properties")返回的第一件事是

<m:properties>
    <d:ServerRelativeUrl>/DesktopApplications/Shared Documents</d:ServerRelativeUrl>
</m:properties>

因此尝试同时设置其他属性会抛出对象引用异常,因为其他元素不在这里。

如何解析这个XML,以便我想要的所有值都放在一个对象中?我不必单独调用来获取每个列表的根文件夹URL。

更新

我在下面发布了一个答案,但我觉得在某个地方有更好的方法。如果它比我的更好,请随意发表答案,我会将你的答案标记为答案。

2 个答案:

答案 0 :(得分:0)

你想要做的是在获得它之前检查元素是否存在,因为此时你只是假设它们将永远存在。

此前有人问过这个问题(我没有足够的声誉将此作为评论发布): Check if an element exists when parsing XML

答案 1 :(得分:0)

我找到了一种方法来获取我需要的东西,但我觉得必须有更好的方法......

private readonly XNamespace a = "http://www.w3.org/2005/Atom";
private readonly XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
private readonly XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";

List<KLList> lists = doc.Descendants(a + "entry").Where(element => element.Attribute(m + "etag") != null).Select(
    list => new KLList()
    {
        Id = list.Descendants(d + "Id").FirstOrDefault().Value,
        Title = list.Descendants(d + "Title").FirstOrDefault().Value,
        ListItemEntityTypeFullName = list.Descendants(d + "ListItemEntityTypeFullName").FirstOrDefault().Value,
        BaseType = (BaseType)Convert.ToInt32(list.Descendants(d + "BaseType").FirstOrDefault().Value),
        ListTemplateType = (ListTemplateType)Convert.ToInt32(list.Descendants(d + "BaseTemplate").FirstOrDefault().Value),
        RelativeUrl = list.Descendants(d + "ServerRelativeUrl").FirstOrDefault().Value
    }).ToList();