Mega Code Archive

 
Categories / C# Tutorial / Windows
 

Query docx properties

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.Office.DocumentFormat.OpenXml; using Microsoft.Office.DocumentFormat.OpenXml.Packaging; using System.Xml.Linq; using System.IO; public class MainClass{    public static void Main(string[] args){                             WordprocessingDocument WD = WordprocessingDocument.Open("a.docx", false);             OpenXmlPackage CustFileProps = WD.CustomFilePropertiesPart.OpenXmlPackage;             IEnumerable<IdPartPair> Parts = from ThisPart in CustFileProps.Parts                where ThisPart.OpenXmlPart.Uri.OriginalString == "/docProps/custom.xml"                select ThisPart;             TextReader PropStream = new StreamReader(Parts.ElementAt(0).OpenXmlPart.GetStream());             XDocument PropDoc = XDocument.Load(PropStream);             IEnumerable<XElement> Props =                from ThisProp in PropDoc.Document.Element(                   XName.Get("Properties",                       "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties")).Elements()                select ThisProp;                             foreach (var ThisProp in Props){                Console.WriteLine(ThisProp.Attribute(XName.Get("name", "")).Value +":\r\n" + ThisProp.Elements().ElementAt(0).Value+ "\r\n");             }             PropStream.Close();             WD.Close();    } }