Convert XmlElement to XElement (Extension Method)

I was using some web-services that returned there values in an XmlElement. I needed to convert this XmlElement to something that I can use with LINQ. I didn't find good solutions on the internet. So I started building my own convert logic. After all kinds of solutions I ended up with creating an new XmlDocument, adding the XmlElement to the XmlDocument and then convert the innerXml of the XmlDocument to an XElement. Not really nice but it does the trick.

I've rewritten the code into a Extension Method for the XmlElement.

[code:c#]

public static XElement ToXElement(this XmlElement xml)
{
   XmlDocument doc = new XmlDocument();

   doc.AppendChild(doc.ImportNode(xml, true));

   return XElement.Parse(doc.InnerXml);

}

[/code]


You use the Extension Method like this:

[code:c#]

XmlElement xmlElement = wsClient.DoWebServiceRequest();
XElement xml = xmlElement.ToXElement();

[/code]


Please tell me if you have a better way of doing this!

Cheers,
Pieter

Asp.Net Membership: Set url to the login page

The loginstatus control doesn't have a property to set the url of the login page. You need to set this URL in the web.config of your application.

Insert the login URL between the authentication node of the web.config. So it looks like this

[code:c#]

<authentication mode="Forms">
 <forms loginUrl="~/pages/login.aspx" />
</authentication>

[/code]

Change the value of the loginUrl to your login page.

Run website on IIS7 (Vista) in classic mode

If you run your website on IIS7 you can run into the following HTTP errors: 500.22, 500.23

[code:html]

An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode.

[/code]


If you don't want to change your web.config and still want to run the website on IIS7 you have to change the Application Pool of your Website.

Open IIS, go to your Application Pools, find your Application Pool and double click the Managed PipeLine Mode.

 

Set the Manage Pipeline mode from Integrated to Classic.


Set IIS7 Application Pool Defaults
You can also set the Manage pipeline mode in the Application Pool Defaults. You can do this in IIS7 by clicking on Set Application Pool Defaults.

 

And then change the Managed Pipline Mode to Classic.

 


The other way is changing your web.config to work with IIS7. You can get more information about this by reading this post.

 

Silverlight: Add .xap as MIME Type to IIS

When I tried to run a my Silverlight App on IIS7 I got the following error:

[code:html]

Microsoft JScript runtime error: Sys.InvalidOperationException: InitializeError error #2104 in control 'Xaml1': Could not download the Silverlight application. Check web server settings

[/code]


The solution for this problem is adding .XAP file extension to your IIS MIME Types. The MIME type is application/x-silverlight-app.

If you don't know what a MIME Type is read this post.

Load rss (xml) with Linq to XML on medium trust hosting

For a new project I needed to implement show data from RSS. The site will be hosted on a shared hosting environment with medium trust.

I load the rss with the following code:

[code:c#]

XElement rssFeed = XElement.Load("[REPLACE WITH URL TO RSS]");
var posts = from item in rssFeed.Descendants("item")
                  select new
                  {
                       Title = item.Element("title").Value,
                       Description = item.Element("description").Value
                  };

[/code]


This normally works fine for me… But apparently not with medium trust :-(! I got the following error.

[code:c#]

Security Exception

Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

[/code]


After reading a bit and playing with the web.config setting <TRUST /> I found the solution. The solution is putting a ".*" in the originUrl parameter of the trust node.

[code:c#]

<trust level="Medium" originUrl=".*" />

[/code]


So an easy solution for a annoying problem what took me to much time. So I hope this post will help somebody out there 🙂

Rabobank starterscommunity live

This week we launched the Rabostarterscommunity. This community is build with Telligent Community Server 2008 and SiteCore 5.

For reporting we uses Harvest reporting service and Google Analytics. 

One thing I learned was that the support of Telligent is not good and also the first releases of CS2008 was buggy, some parts didn’t work at all!!! And don’t even get me started about Telligent Harvest Reporting (Just don’t use it).

If you need to start a community be sure to check if you really need community server! If you only need a forum use: Yetanotherforum.