我正在尝试从 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是空的。
我做错了什么?
感谢您的任何提示...
请您参考如下方法:
选择元素时需要考虑 XML 中的命名空间,可以使用 XNamespace声明并使用它 - 这有效:
XNamespace xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
var Paras = root.Descendants(xmlns + "Paragraph");
