从 silverlight 富文本框中提取纯文本 - LINQ to XML

发布于 2024-11-11 07:10:09 字数 969 浏览 3 评论 0原文


我正在尝试从 SL 4 富文本框的 xaml 内容中获取纯文本。
内容如下所示:

<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>

当我尝试此操作时:

            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 为空。
我做错了什么?
感谢您的任何提示...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌梦深 2024-11-18 07:10:09

选择元素时需要考虑 XML 中的命名空间,可以使用 XNamespace 来声明和使用它 - 这有效:

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

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");
难忘№最初的完美 2024-11-18 07:10:09

感谢 BrokenGlass。完整功能:

string StringFromRichTextBox(string XAML)
    {
        XElement root = XElement.Parse(XAML);
        XNamespace xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
        StringBuilder sb = new StringBuilder();
        var Paras = root.Descendants(xmlns + "Paragraph");            
        foreach (XElement para in Paras)
        {
            foreach (XElement run in Paras.Descendants(xmlns + "Run"))
            {
                XAttribute a = run.Attribute("Text");
                sb.Append(null != a ? (string)a : "");
            }
        }
        return sb.ToString();
    }

成功了!希望这对您有帮助。
阮明贤

Thanks to BrokenGlass.The full function:

string StringFromRichTextBox(string XAML)
    {
        XElement root = XElement.Parse(XAML);
        XNamespace xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
        StringBuilder sb = new StringBuilder();
        var Paras = root.Descendants(xmlns + "Paragraph");            
        foreach (XElement para in Paras)
        {
            foreach (XElement run in Paras.Descendants(xmlns + "Run"))
            {
                XAttribute a = run.Attribute("Text");
                sb.Append(null != a ? (string)a : "");
            }
        }
        return sb.ToString();
    }

It worked! Hope this help you.
Nguyen Minh Hien

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文