GData 电子表格和 GSX 命名空间

发布于 2024-11-01 14:48:18 字数 3043 浏览 1 评论 0原文

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

使用列表 API 读取电子表格的内容后,我获得了作为 XElement 形式的单独行,

<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 参数。谁能帮我解决这个问题吗?

I'm trying to read Google Spreadsheet programatically. Unfortunately, I can't use GData libraries because the platform is Windows Phone.

After reading the content of the spreadsheet using the list API, I get indidual rows as XElements of form

<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>

I'm interested only in elements starting with namespace. So I declared the namespace like this and check every descendant if it matches it.

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);
                }
            }

I'm sure there is an elegant way by specifying this filter as an XName parameter to xe.Descendants() method. Can anyone help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2024-11-08 14:48:18

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

xe.Elements().Where(el => el.Name.Namespace == gsx) should give you all child elements of xe which are in the gsx namespace you have defined. So that is what is possible, instead of putting an if statement into the foreach loop you can simply filter the Elements (or Descendants if needed) with a Where method call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文