How to read xml from web page and bind the data to Silverlight module in SharePoint 2007 Part-2

Some times we have requirements in SharePoint that we need to pull images from
SharePoint library and display those images in Silverlight module with rich UI.
But we know that Silverlight runs on client side, we can't access the SharePoint
libraries in the Silverlight projects because of permission issues as
well.
So, here we build an architecture that SharePoint will build the XML
for us and write it on the page and Silverlight grabs the XML from the page and
displays the images on the UI. How nice it is. You can get the first part i.e.
get xml (image) data from the SharePoint libraries How to show images in Silverlight by reading images from SharePoint 2007 library Part-1.

Here,
we are discussing about the code we need to place in Page.xaml.cs file to read
xml from web page.

  1. public Page()
     

  2. {  

  3. string controlid = "divImgs"; //You can also use intiparams of Silverlight params to get the id.
     

  4. InitializeComponent();  

  5.  

  6. string xmlstring = string.Empty;  

  7. if (controlid != null)  

  8. {  

  9. HtmlElement ctl = HtmlPage.Document.GetElementById(controlid);
     

  10. if (ctl != null)  

  11. xmlstring = (string)ctl.GetProperty("innerHTML");  

  12. }  

  13.  

  14. if (!string.IsNullOrEmpty(xmlstring))
     

  15. {  

  16. ProcessXML(xmlstring);  

  17. }  

The above code is the Page constructor of the Page. Xaml.cs file.
1.
Here, we are catching the control on the HTML document and reading it's HTML. if
you remember, while coverting the data view web part data from HTML to XML, in
the XSLT i assigned an id for a html tag called "divImgs". We are using this id
here in the cs file, and reading the HTML, obviously it will get the xml data to
the xmlstring variable.
2. No, we need to process the XML and bind the data
to the silverlight control. This is why i am calling a function called "Process
XML".

  1. private void ProcessXML(string xml)  

  2. {  

  3. images = new List<string>();  

  4. if (xml != string.Empty)  

  5. {  

  6. try 

  7. {  

  8. StringReader textStream = null;  

  9. textStream = new StringReader(xml);  

  10.  

  11. if (textStream != null)  

  12. {  

  13. using (XmlReader reader = XmlReader.Create(textStream))
     

  14. {  

  15. while (!reader.EOF)
     

  16. {  

  17. if ((reader.IsStartElement()) && (reader.LocalName == "slides"))  

  18. {  

  19. if (reader.HasAttributes)  

  20. {  

  21. reader.MoveToAttribute("baseUrl");  

  22. }  

  23. }  

  24. else 

  25. {  

  26. if ((reader.LocalName == "slide") && (reader.HasAttributes))
     

  27. {  

  28. reader.MoveToAttribute("imageUrl");  

  29. string imageName = reader.Value.ToLower();
     

  30. if ((imageName.Contains(".jpg")  

  31. || imageName.Contains(".png")))  

  32. images.Add(reader.Value);  

  33. }  

  34.  

  35. }  

  36. reader.Read();  

  37. }  

  38. }  

  39. }  

  40. }  

  41. catch (Exception ex)
     

  42. {  

  43. }  

  44. }  

3. In the above code, images is the global variable of type
List<string>. We are filling this object with the image urls by reading
the xml string.
4. Now by calling the ProcessXML function, we are done with
getting image urls. So we have collection of image urls, and use this object to
give input as the Silverlight module controls and display on the UI.