Still looking for a sponsor Max Paulousky is looking for a Silverlight/.Net job in the Commonwealth

Share to Facebook Tweet this! Share to MySpace Share to Google Share to Live   Share via AddThis

How to get Xml InnerText in a Silverlight application

The Silverlight framework is restricted in comparison with .Net framework. It does not provide the XmlDocument class, that has the InnerText property. Silverlight applications should use XDocument instead of XmlDocument. Following code is an extension method for the XNode class that implements InnerText functionality.

public static string InnerText(this XNode xNode)
{
  bool isContainer = xNode is XContainer;
  if (!isContainer)
  {
    switch (xNode.NodeType)
    {
      case XmlNodeType.Text:
      case XmlNodeType.CDATA:
      case XmlNodeType.Whitespace:
      case XmlNodeType.SignificantWhitespace:
        return xNode.ToString();
      default:
        return string.Empty;
    }
  }
  else
  {
    XContainer xContainer = (XContainer)xNode;
    StringBuilder sb = new StringBuilder();
 
    for (XNode node = xContainer.FirstNode; node != null; node = node.NextNode)
      sb.Append(node.InnerText());
 
    return sb.ToString();
  }
}

XContainer is a type of xml nodes that contain child nodes and they should be processed recursively.

This work is licensed under a Creative Commons Attribution By license.

Leave a Comment [ RSS ]

Have Your Say »

(will not be published)

XHTML: You can use these tags: <a href="" title=""> <blockquote cite=""> <code> <em> <p> <strike> <strong> <sub> <super> <u>

Please add 6 and 1 and type the answer here:

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.