openxml – 如何以编程方式访问open xml worddoc文件的内置属性
发布时间:2020-12-16 23:21:22 所属栏目:百科 来源:网络整理
导读:我想访问一个打开的xml word doc文件的一些内置属性(如作者,上次修改日期等).我想为此目的使用open xml sdk2.0.所以我想知道是否有任何类或任何方式我可以编程访问这些内置属性. 解决方法 可以在 here找到对以下方法的解释,但几乎需要将要从core.xml文件中获
我想访问一个打开的xml word doc文件的一些内置属性(如作者,上次修改日期等).我想为此目的使用open xml sdk2.0.所以我想知道是否有任何类或任何方式我可以编程访问这些内置属性.
解决方法
可以在
here找到对以下方法的解释,但几乎需要将要从core.xml文件中获取的属性传递给此方法,它将返回值:
public static string WDRetrieveCoreProperty(string docName,string propertyName) { // Given a document name and a core property,retrieve the value of the property. // Note that because this code uses the SelectSingleNode method,// the search is case sensitive. That is,looking for "Author" is not // the same as looking for "author". const string corePropertiesSchema = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties"; const string dcPropertiesSchema = "http://purl.org/dc/elements/1.1/"; const string dcTermsPropertiesSchema = "http://purl.org/dc/terms/"; string propertyValue = string.Empty; using (WordprocessingDocument wdPackage = WordprocessingDocument.Open(docName,true)) { // Get the core properties part (core.xml). CoreFilePropertiesPart corePropertiesPart = wdPackage.CoreFilePropertiesPart; // Manage namespaces to perform XML XPath queries. NameTable nt = new NameTable(); XmlNamespaceManager nsManager = new XmlNamespaceManager(nt); nsManager.AddNamespace("cp",corePropertiesSchema); nsManager.AddNamespace("dc",dcPropertiesSchema); nsManager.AddNamespace("dcterms",dcTermsPropertiesSchema); // Get the properties from the package. XmlDocument xdoc = new XmlDocument(nt); // Load the XML in the part into an XmlDocument instance. xdoc.Load(corePropertiesPart.GetStream()); string searchString = string.Format("//cp:coreProperties/{0}",propertyName); XmlNode xNode = xdoc.SelectSingleNode(searchString,nsManager); if (!(xNode == null)) { propertyValue = xNode.InnerText; } } return propertyValue; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |