GData 电子表格和 GSX 命名空间
我正在尝试以编程方式读取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 ofxe
which are in thegsx
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.