я хотел бы получить доступ к некоторым встроенным свойствам (таким как автор, дата последнего изменения и т. д.) открытого файла документа xml word. я хотел бы использовать open xml sdk2.0 для этой цели. поэтому мне интересно, есть ли какой-либо класс или какой-либо способ программно получить доступ к этим встроенным свойствам.
как программно получить доступ к встроенным свойствам открытого файла xml worddoc
comment
Посмотрите на этот код, чтобы найти несколько полезных советов: https://searchcode.com/codesearch/view/10033886/ а>.
- person AnorZaken   schedule 08.01.2016
Ответы (3)
Объяснение следующего метода можно найти здесь, но в значительной степени вам нужно передать свойства, которые вы хотите получить из файла 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;
}
person
amurra
schedule
14.12.2010
Вы также можете использовать API упаковки:
using System.IO.Packaging.Package;
[...]
using (var package = Package.Open(path))
{
package.PackageProperties.Creator = Environment.UserName;
package.PackageProperties.LastModifiedBy = Environment.UserName;
}
Это работает и для других открытых форматов XML, таких как Power Point.
person
JanDotNet
schedule
20.07.2016
отлично! я использовал для xlsx
- person elle0087; 11.10.2017
Спасибо! Я знаю, что это не часть вопроса, но чтобы сохранить свойства, измененные в этом примере, используйте: package.Close();
- person Cesar; 07.08.2018
пакет.Сохранить(); Затем package.closed;
Я думаю, что это лучший способ.
person
Mattias Jarl
schedule
23.09.2019