Switch to fluid Switch to wfix Switch to fixed

StackPanel

dynamic rants = new Stack();

Providing InnerXml to XElement

Posted by Vin on Aug-20-2008

In my latest project, I dived in to the world of Linq, by using Linq to Sql. As I have loads of XML related application logic I also got to use Linq to XML. We have XElement class given to us from System.Xml.Linq namespace which has loads of usefulness over the old world XmlElement class. So there I go admiring XElement, but wait I don’t have an InnerXml method, that lot of us have gotten used since the MSXML DOM days.

I came up with providing an Extension method InnerXml() on XElement.

public static string InnerXml(this XElement element)
{
    StringBuilder innerXml = new StringBuilder();

    foreach (XNode node in element.Nodes())
    {
        // append node's xml string to innerXml
        innerXml.Append(node.ToString());
    }
    return innerXml.ToString();
}

Someone was exactly looking for this on StackOverflow (the awesome tech QnA site that is in private beta right now)

Add A Comment