从 silverlight 富文本框中提取纯文本 - LINQ to XML
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选择元素时需要考虑 XML 中的命名空间,可以使用
XNamespace
来声明和使用它 - 这有效:You need to account for the namespace in your XML when selecting elements, you can use
XNamespace
to declare and use it - this works:感谢 BrokenGlass。完整功能:
成功了!希望这对您有帮助。
阮明贤
Thanks to BrokenGlass.The full function:
It worked! Hope this help you.
Nguyen Minh Hien