开发者

GData Spreadsheet and GSX Namespaces

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/extend开发者_JAVA技巧ed">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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜