Reputation: 2694
I have a word Document in Xml format with multiple entries like so :
<aml:annotation aml:id="0" w:type="Word.Bookmark.Start" w:name="CustomerName"/>
I want to retrieve a collection of these but cannot see how to get past
foreach (XElement ann in doc.Root.Descendants(aml + "annotation"))
In other words I can get all annotations, but cannot see how to filter to just retrieve bookmarks. The namespaces aml
and w
are declared like this
XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
XNamespace aml = "http://schemas.microsoft.com/aml/2001/core";
Could someone give me a push ?
I resolved the issue as follows
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))
{
...
}
Upvotes: 0
Views: 932
Reputation: 464
This solution is not for XML but maybe helps you.
System.Collections.Generic.IEnumerable<BookmarkStart> BookMarks = wordDoc.MainDocumentPart.RootElement.Descendants<BookmarkStart>();
foreach (BookmarkStart current in BookMarks)
{
//Do some...
}
Upvotes: 0
Reputation: 236308
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 syntax:
doc.Root.Descendants(aml + "annotation")
.Where(a => (string)a.Attribute(w + "type") == "Word.Bookmark.Start")
.Select(a => (string)a.Attribute(w + "name"))
Upvotes: 1