두 가지 책갈피 사이의 텍스트에 주로 관심이있는 경우이 작업은 사용자가 원하는 작업에 따라 다르지만 LINQ to XML이나 강력하게 XmlDocument/XPath 의미를 사용하는 것이 더 쉬운 경우 중 하나입니다 Open XML SDK V2의 형식화 된 개체 모델입니다. XPath의 'following :: *'축의 의미는 사용자가 원하는 것입니다. 다음 예제에서는 XmlDocument 및 XPath를 사용하여 책갈피의 시작과 끝 사이에 노드 이름을 인쇄합니다.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
class Program
{
public static XmlDocument GetXmlDocument(OpenXmlPart part)
{
XmlDocument xmlDoc = new XmlDocument();
using (Stream partStream = part.GetStream())
using (XmlReader partXmlReader = XmlReader.Create(partStream))
xmlDoc.Load(partXmlReader);
return xmlDoc;
}
static void Main(string[] args)
{
using (WordprocessingDocument doc =
WordprocessingDocument.Open("Test.docx", false))
{
XmlDocument xmlDoc = GetXmlDocument(doc.MainDocumentPart);
string wordNamespace =
"http://schemas.openxmlformats.org/wordprocessingml/2006/main";
XmlNamespaceManager nsmgr =
new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("w", wordNamespace);
XmlElement bookmarkStart = (XmlElement)xmlDoc.SelectSingleNode("descendant::w:bookmarkStart[@w:id='0']", nsmgr);
XmlNodeList nodesFollowing = bookmarkStart.SelectNodes("following::*", nsmgr);
var nodesBetween = nodesFollowing
.Cast<XmlNode>()
.TakeWhile(n =>
{
if (n.Name != "w:bookmarkEnd")
return true;
if (n.Attributes.Cast<XmlAttribute>().Any(a => a.Name == "w:id" && a.Value == "0"))
return false;
return true;
});
foreach (XmlElement item in nodesBetween)
{
Console.WriteLine(item.Name);
if (item.Name == "w:bookmarkStart" || item.Name == "w:bookmarkEnd")
foreach (XmlAttribute att in item.Attributes)
Console.WriteLine("{0}:{1}", att.Name, att.Value);
}
}
}
}