Search code examples
c#silverlightpivotviewercxml-collectionxml

Pivotviewer's .cxml parsing


I'm trying to do very simple operations on a .cxml file. As you know it's basically an .xml file. This is a sample file I created to test the application:

<?xml version="1.0" encoding="utf-8"?>
<Collection xmlns:p="http://schemas.microsoft.com/livelabs/pivot/collection/2009" SchemaVersion="1.0" Name="Actresses" xmlns="http://schemas.microsoft.com/collection/metadata/2009">
  <FacetCategories>
    <FacetCategory Name="Nationality" Type="LongString" p:IsFilterVisible="true" p:IsWordWheelVisible="true" p:IsMetaDataVisible="true" />
  </FacetCategories>
<!-- Other entries-->
  <Items ImgBase="Actresses_files\go144bwo.0ao.xml" HrefBase="http://www.imdb.com/name/">    
    <Item Id="2" Img="#2" Name="Anna Karina" Href="nm0439344/">
      <Description> She is a nice girl</Description>
      <Facets>
        <Facet Name="Nationality">
          <LongString Value="Danish" />
        </Facet>
      </Facets>
    </Item>    
  </Items>
<!-- Other entries-->
</Collection>

I can't get any functioning simple code like:

XDocument document = XDocument.Parse(e.Result);
foreach (XElement x in document.Descendants("Item"))
{
...
}

The test on a generic xml is working. The cxml file is correctly loaded in document.

While watching the expression:

document.Descendants("Item"), results

the answer is:

Empty "Enumeration yielded no results" string

Any hint on what can be the error? I've also add a quick look to get Descendants of Facet, Facets, etc., but there are no results in the enumeration. This obviously doesn't happen with a generic xml file I used for testing. It's a problem I have with .cxml.


Solution

  • Basically your XML defines a default namespace with the xmlns="http://schemas.microsoft.com/collection/metadata/2009" attribute:

    That means you need to fully qualify your Descendants query e.g.:

    XDocument document = XDocument.Parse(e.Result);
    foreach (XElement x in document.Descendants("{http://schemas.microsoft.com/collection/metadata/2009}Item"))
    {
    ...
    }
    

    If you remove the default namespace from the XML your code actually works as-is, but that is not the aim of the exercise.