Extract plain text from the rich silverlight text box - LINQ to XML

advertisements

I am trying to get the plain text from the xaml content of the SL 4 rich text box.
The content looks like this:

<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">
    <Paragraph FontSize=\"12\" FontFamily=\"Arial\" Foreground=\"#FF000000\" FontWeight=\"Normal\" FontStyle=\"Normal\" FontStretch=\"Normal\" TextAlignment=\"Left\">
         <Run Text=\"Biggy\" />
    </Paragraph>
</Section>

When I try this:

            XElement root = XElement.Parse(xml);
            var Paras = root.Descendants("Paragraph");
            foreach (XElement para in Paras)
            {
                foreach (XElement run in Paras.Descendants("Run"))
                {
                    XAttribute a = run.Attribute("Text");
                    text += null != a ? (string) a : "";
                }
            }

Paras is empty.
What am I doing wrong?
Thanks for any hints...


You need to account for the namespace in your XML when selecting elements, you can use XNamespace to declare and use it - this works:

XNamespace xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
var Paras = root.Descendants(xmlns + "Paragraph");