【问题标题】:GData Spreadsheet and GSX NamespacesGData 电子表格和 GSX 命名空间
【发布时间】:2011-04-16 12:31:06
【问题描述】:

我正在尝试以编程方式阅读 Google 电子表格。不幸的是,我不能使用 GData 库,因为平台是 Windows Phone。

在使用列表 API 读取电子表格的内容后,我将单个行作为表单的 XElements 获得

<entry xmlns="http://www.w3.org/2005/Atom">
  <id>https://spreadsheets.google.com/feeds/list/0At7MazQVk6r1dHNOLS02MmVONDdLSDRNSjVPcUZRb0E/1/private/values/chk2m</id>
  <updated>2011-04-16T06:23:48.922Z</updated>
  <category scheme="http://schemas.google.com/spreadsheets/2006" term="http://schemas.google.com/spreadsheets/2006#list" />
  <title type="text">0002</title>
  <content type="text">ssname: Some random address, latitude: 2.8595084738555, longtitude: 167.0312830513769, fuel: x, maintenance_2: x</content>
  <link rel="self" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/list/0At7MazQVk6r1dHNOLS02MmVONDdLSDRNSjVPcUZRb0E/1/private/values/chk2m" />
  <gsx:stationid xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">PK0002</gsx:stationid>
  <gsx:ssname xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">Svs Stn</gsx:ssname>
  <gsx:address xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">Some random address</gsx:address>
  <gsx:tel xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">12345678</gsx:tel>
  <gsx:latitude xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">1.35</gsx:latitude>
  <gsx:longtitude xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">103.80312830513769</gsx:longtitude>
  <gsx:operatinghours xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended"></gsx:operatinghours>
  <gsx:fuel xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">x</gsx:fuel>
  <gsx:fuel_2 xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended"></gsx:fuel_2>
  <gsx:maintenance xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended"></gsx:maintenance>
  <gsx:maintenance_2 xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">x</gsx:maintenance_2>
  <gsx:amenities xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended"></gsx:amenities>
  <gsx:amenities_2 xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended"></gsx:amenities_2>
</entry>

我只对以命名空间开头的元素感兴趣。所以我这样声明了命名空间,并检查每个后代是否匹配。

XNamespace gsx = "http://schemas.google.com/spreadsheets/2006/extended";
            IEnumerable<XElement> allItems = xe.Descendants();
            foreach (XElement xItem in allItems)
            {
                if (xItem.Name.NamespaceName == gsx.NamespaceName)
                {
                    Debug.WriteLine("$$$" + xItem);
                }
                else
                {
                    Debug.WriteLine("XXX" + xItem);
                }
            }

我确信通过将此过滤器指定为 xe.Descendants() 方法的 XName 参数是一种优雅的方法。谁能帮我解决这个问题?

【问题讨论】:

    标签: c# linq windows-phone-7 linq-to-xml


    【解决方案1】:

    xe.Elements().Where(el =&gt; el.Name.Namespace == gsx) 应该为您提供 xe 的所有子元素,它们位于您定义的 gsx 命名空间中。所以这是可能的,而不是将 if 语句放入 foreach 循环中,您可以简单地使用 Where 方法调用过滤元素(或后代,如果需要)。

    【讨论】:

      猜你喜欢
      • 2011-11-05
      • 2013-12-13
      • 2010-10-20
      • 2012-07-20
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多