我有一个Xml格式的文档,其中包含多个条目:
<aml:annotation aml:id="0" w:type="Word.Bookmark.Start" w:name="CustomerName"/>
我想要检索这些的集合,但看不到如何过去
foreach (XElement ann in doc.Root.Descendants(aml + "annotation"))
换句话说,我可以获得所有注释,但无法看到如何过滤以仅检索书签。名称空间aml
和w
声明为此
XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
XNamespace aml = "http://schemas.microsoft.com/aml/2001/core";
有人能帮我推吗?
我解决了以下问题
XNamespace w = doc.Root.GetNamespaceOfPrefix("w");
XNamespace aml = doc.Root.GetNamespaceOfPrefix("aml");
foreach (string bookm in doc.Descendants(aml + "annotation")
.Where(e => e.Attributes(w + "type")
.Any(a => a.Value == "Word.Bookmark.Start"))
.Select(b => b.Attribute(w + "name").Value))
{
...
}
答案 0 :(得分:1)
var names = from a in doc.Root.Descendants(aml + "annotation"))
where (string)a.Attribute(w + "type") == "Word.Bookmark.Start"
select (string)a.Attribute(w + "name");
Lambda语法:
doc.Root.Descendants(aml + "annotation")
.Where(a => (string)a.Attribute(w + "type") == "Word.Bookmark.Start")
.Select(a => (string)a.Attribute(w + "name"))
答案 1 :(得分:0)
此解决方案不适用于XML,但可能对您有所帮助。
System.Collections.Generic.IEnumerable<BookmarkStart> BookMarks = wordDoc.MainDocumentPart.RootElement.Descendants<BookmarkStart>();
foreach (BookmarkStart current in BookMarks)
{
//Do some...
}