Open XML のプログラミング

スクリーンキャスト

このデモの内容

ここでは、2007 Microsoft Office system の標準フォーマットであるOpen XML をプログラムから利用する手法やポイントについてご紹介します。

デモでご紹介しているソースコード

【コンソールアプリケーション (C#)】

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Packaging;
using System.Xml;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmlNS = @"http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-07-18T03:20:17";
            string xmlDocRelType = @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml";
            string officeDocRelType = @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";

            PackagePart documentPart = null;
            PackagePart customxmlPart = null;
            Uri documentUri = null;
            Uri customxmlUri = null;

            // Prompt user
            Console.WriteLine("Please input your name.");
            string paramName = Console.ReadLine();
            Console.WriteLine("Please input your birth (yyyy-mm-dd).");
            string paramBirth = Console.ReadLine();

            // Open the docx container as a System.IO.Packaging.Package.
            using (Package docPackage = Package.Open(@"C:\Demo\sample.docx", FileMode.Open, FileAccess.ReadWrite))
            {
                // find the "documentRoot" part
                foreach (PackageRelationship relationship in docPackage.GetRelationshipsByType(officeDocRelType))
                {
                    documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);
                    documentPart = docPackage.GetPart(documentUri);
                    break;  // document root part will be one, so exit soon !
                }

                //find the "customXML" part
                foreach (PackageRelationship relationship in documentPart.GetRelationshipsByType(xmlDocRelType))
                {
                    customxmlUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);
                    if (CheckNamespace(docPackage.GetPart(customxmlUri), xmlNS) == true)
                    {
                        customxmlPart = docPackage.GetPart(customxmlUri);
                        break;
                    }
                }

                // Change values !
                XmlDocument customxmlXml = new XmlDocument();
                customxmlXml.Load(customxmlPart.GetStream());
                XmlNamespaceManager customxmlXmlNSMgr = new XmlNamespaceManager(customxmlXml.NameTable);
                customxmlXmlNSMgr.AddNamespace("my", xmlNS);
                XmlNode nodeName = customxmlXml.SelectSingleNode("/my:myFields/my:Name", customxmlXmlNSMgr);
                nodeName.InnerText = paramName;
                XmlNode nodeBirth = customxmlXml.SelectSingleNode("/my:myFields/my:Birth", customxmlXmlNSMgr);
                nodeBirth.InnerText = paramBirth;

                // Save !
                customxmlXml.Save(customxmlPart.GetStream(FileMode.Create, FileAccess.Write));
            }
        }

        static bool CheckNamespace(PackagePart thisPart, string matchNameSpace)
        {
            Stream outputStream = thisPart.GetStream(FileMode.Open, FileAccess.ReadWrite);
            StreamReader readStream = new StreamReader(outputStream);
            XmlDocument thisDoc = new XmlDocument();
            thisDoc.LoadXml(readStream.ReadToEnd());

            if (thisDoc.DocumentElement.NamespaceURI == matchNameSpace)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}

![](images/cc707258.top(ja-jp, MSDN.10).gif)ページのトップへ