Freigeben über


Using the OpenXmlDocument Class

Now, we can write a small program to use our new class, to dump the relationships to the console:

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Examples.LtxOpenXml;

class Program
{
static void DumpRelationships(List<Relationship> source, int indentLevel)
{
string indent = "".PadRight(indentLevel * 2);

        foreach (var r in source)
{
Console.WriteLine("{0}Id: {1}",
indent, r.Id);
Console.WriteLine("{0}RelationshipType: {1}",
indent, r.RelationshipType);
Console.WriteLine("{0}ContentType: {1}",
indent, r.ContentType);
Console.WriteLine("{0}SourceUri: {1}",
indent, r.SourceUri);
Console.WriteLine("{0}TargetUri: {1}",
indent, r.TargetUri);
Console.WriteLine("{0}TargetMode: {1}",
indent, r.TargetMode);
if (r.XDocument != null)
Console.WriteLine("{0}XDocument descendant node count: {1}",
indent, r.XDocument.DescendantNodes().Count());
Console.WriteLine();
if (r.Relationships != null)
DumpRelationships(r.Relationships, indentLevel + 1);
}
}

    static void Main(string[] args)
{
string filename = "OfficeXMLMarkupExplained_en.docx";
//string filename = "Test.docx";
//string filename = "Book1.xlsx";

        using (OpenXmlDocument doc = new OpenXmlDocument(filename))
{
Console.WriteLine("Document Name: {0}", doc.Name);
Console.WriteLine();
DumpRelationships(doc.Relationships, 0);
}
}
}

This code produces very similar output to the first example.

Comments

  • Anonymous
    March 31, 2008
    Theseresourceisforpreparingtheproject,supervisedbyMONOandGOOGLESUMMERCODE2008,Converti...

  • Anonymous
    March 31, 2008
    These resource is for preparing the project ,supervised by MONO and GOOGLE SUMMER CODE 2008,Converting

  • Anonymous
    November 14, 2008
    (November 14, 2008 - I've updated my approach for querying Open XML documents using LINQ to XML. You