Geeks With Blogs
Keep It Simple Simple Solutions for Complex Scenarios

Yesterday working in a demo about WCF-REST Starter Kit (http://www.asp.net/downloads/starter-kits/wcf-rest/) i had a little issue to make a query to a XML Structure that includes a xml namespace (aka xmlns), the trick is very simple to get the value from each element in the structure, you should pass the XName in Elements method including the XNamespace

This is my XML structure

<ItemInfoList xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
- <ItemInfo>
  <EditLink>http://localhost:17673/Services/Courses.svc/S02</EditLink> 
- <Item xmlns:a="http://schemas.datacontract.org/2004/07/DotNetUniversity">
  <a:Code>S02</a:Code> 
  <a:Description>La Transferencia de Estado Representacional (Representational State Transfer) o REST es una técnica de arquitectura software para sistemas hipermedia distribuidos como la World Wide Web. El término se originó en el año 2000, en una tesis doctoral sobre la web escrita por Roy Fielding, uno de los principales autores de la especificación del protocolo HTTP y ha pasado a ser ampliamente utilizado por la comunidad de desarrollo.</a:Description> 
  <a:Name>REST</a:Name> 
  <a:OpenDate i:nil="true" /> 
  </Item>
  </ItemInfo>
- <ItemInfo>
  <EditLink>http://localhost:17673/Services/Courses.svc/S01</EditLink> 
- <Item xmlns:a="http://schemas.datacontract.org/2004/07/DotNetUniversity">
  <a:Code>S01</a:Code> 
  <a:Description>La Arquitectura Orientada a Servicios (en inglés Service Oriented Architecture), es un concepto de arquitectura de software que define la utilización de servicios para dar soporte a los requisitos del negocio.</a:Description> 
  <a:Name>SOA</a:Name> 
  <a:OpenDate i:nil="true" /> 
  </Item>
  </ItemInfo>
  </ItemInfoList>

My custom collection

   1:  private ObservableCollection<CourseViewModel> courses = new ObservableCollection<CourseViewModel>();

 

This is the code to query this structure and populate a collection of a custom entities, BTW i’m using Silverlight 3 with MVVM approach

   1:  public void LoadCatalog()
   2:          {
   3:              courses.Clear();
   4:              WebClient client = new WebClient();
   5:              client.DownloadStringCompleted += (source, args) =>
   6:              {
   7:                  if (args.Error == null)
   8:                  {
   9:                      IEnumerable<XElement> coursesXml = XElement.Load(new StringReader(args.Result)).Descendants("ItemInfo");
  10:                      foreach (var item in coursesXml.Descendants("Item"))
  11:                      {
  12:                          XNamespace ns = "http://schemas.datacontract.org/2004/07/DotNetUniversity";
  13:   
  14:                          bool hasOpenDate = !String.IsNullOrEmpty(item.Element(ns + "OpenDate").Value);
  15:                          DateTime openDate = default(DateTime);
  16:                          if (hasOpenDate)
  17:                          {
  18:                              DateTime.TryParse(item.Element(ns + "OpenDate").Value, out openDate);
  19:                          }
  20:   
  21:                          courses.Add(
  22:                              new CourseViewModel(
  23:                                  new Course()
  24:                                  {
  25:                                      Name = item.Element(ns + "Name").Value,
  26:                                      Code = item.Element(ns + "Code").Value,
  27:                                      Description = item.Element(ns + "Description").Value,
  28:                                      OpenDate = openDate,
  29:                                      OpenDateSpecified = hasOpenDate
  30:                                  }));
  31:                      }
  32:                  }
  33:              };
  34:              client.DownloadStringAsync(new Uri("http://localhost:17673/Services/Courses.svc/"));
  35:          }

 

See you

Posted on Sunday, October 11, 2009 10:50 PM Silverlight | Back to top


Comments on this post: Querying to XML structures that have a namespace

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Juan Mestas J. (aka gotchas) | Powered by: GeeksWithBlogs.net