System.Xml.Linq and Office Open XML

Starting to play with those. So just FYE, here is what I wrote yesterday:

// create the new document.xml package part

Uri documentUri = new Uri(“/word/document.xml”, UriKind.Relative);

PackagePart documentPart = package.CreatePart(documentUri, “application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml”);


XNamespace r = “”;

XNamespace wp = “”;

XNamespace w = “”;

XNamespace dm = “”;

XNamespace dp = “”;


var firstParagraph = new XElement(w.GetName(“p”),

                                  new XElement(w.GetName(“r”),

                                               new XElement(w.GetName(“t”), “A paragraph”)));


var secondParagraph = new XElement(w.GetName(“p”),

                                   new XElement(w.GetName(“r”),

                                                new XElement(w.GetName(“t”),

                                                             new XAttribute(XNamespace.Xml + “space”, “preserve”),

                                                             “A “)),

                                   new XElement(w.GetName(“hyperlink”),

                                                new XAttribute(r.GetName(“id”), “rId2”),

                                                new XElement(w.GetName(“r”),

                                                             new XElement(w.GetName(“rPr”),

                                                                          new XElement(w.GetName(“color”),

                                                                                       new XAttribute(w.GetName(“val”), “0000FF”),

                                                                                       new XAttribute(w.GetName(“themeColor”), “hyperlink”)),

                                                                          new XElement(w.GetName(“u”),

                                                                                       new XAttribute(w.GetName(“val”), “single”))),

                                                             new XElement(w.GetName(“t”), “hyperlink”))));


var graphic = new XElement(dm.GetName(“graphic”),

                           new XAttribute(XNamespace.Xmlns + “dm”, dm.NamespaceName),

                           new XElement(dm.GetName(“graphicData”),

                                        new XAttribute(“uri”, dp.NamespaceName),

                                        new XElement(dp.GetName(“pic”),

                                        new XAttribute(XNamespace.Xmlns + “dp”, dp.NamespaceName),

                                        new XElement(dp.GetName(“nvPicPr”),

                                                     new XElement(dp.GetName(“cNvPr”),

                                                                  new XAttribute(“id”, 0),

                                                                  new XAttribute(“name”, “openxmldeveloper.gif”)),

                                                     new XElement(dp.GetName(“cNvPicPr”))),

                                        new XElement(dp.GetName(“blipFill”),

                                                     new XElement(dm.GetName(“blip”),

                                                                  new XAttribute(r.GetName(“embed”), “rId1”)),

                                                     new XElement(dm.GetName(“stretch”),

                                                                  new XElement(dm.GetName(“fillRect”)))),

                                        new XElement(dp.GetName(“spPr”),

                                                     new XElement(dm.GetName(“xfrm”),

                                                                  new XElement(dm.GetName(“off”),

                                                                               new XAttribute(“x”, 0),

                                                                               new XAttribute(“y”, 0)),

                                                                  new XElement(dm.GetName(“ext”),

                                                                               new XAttribute(“cx”, 4448175),

                                                                               new XAttribute(“cy”, 1181100))),

                                                     new XElement(dm.GetName(“prstGeom”),

                                                                  new XAttribute(“prst”, “rect”),

                                                                  new XElement(dm.GetName(“avLst”)))))));


var drawing = new XElement(w.GetName(“drawing”),

                           new XElement(wp.GetName(“inline”),

                                        new XElement(wp.GetName(“extent”),

                                                     new XAttribute(“cx”, 4448175),

                                                     new XAttribute(“cy”, 1181100)),

                                        new XElement(wp.GetName(“docPr”),

                                                     new XAttribute(“id”, 1),

                                                     new XAttribute(“name”, “Picture 0”),

                                                     new XAttribute(“descr”, “openxmldeveloper.gif”)),

                                        new XElement(wp.GetName(“cNvGraphicFramePr”),

                                                     new XElement(dm.GetName(“graphicFrameLocks”),

                                                                  new XAttribute(XNamespace.Xmlns + “dm”, dm.NamespaceName),

                                                                  new XAttribute(“noChangeAspect”, 1))),



var thirdParagraph = new XElement(w.GetName(“p”),new XElement(w.GetName(“r”),drawing));


var document = new XDocument( new XDeclaration(“1.0”, “UTF-8”, “yes”),

                              new XElement(w.GetName(“document”),

                                           new XAttribute(XNamespace.Xmlns + “r”, r.NamespaceName),

                                           new XAttribute(XNamespace.Xmlns + “wp”, wp.NamespaceName),

                                           new XAttribute(XNamespace.Xmlns + “w”, w.NamespaceName),

                                           new XElement(w.GetName(“body”), firstParagraph, secondParagraph, thirdParagraph)));


using (XmlWriter xmlWriter = XmlWriter.Create(documentPart.GetStream()))


Comments (3)

  1. @ Head says:

    I was catching up on reading some msdn blogs and stumbled across an entry from Yves Dolce. Yves was showing

  2. Bill_McC says:

    Hi Yves,

    Have a look at my implementation in VB. I’d be interested to hear if, given the VB implementation, you think it makes sense to use C# for this, or whether it’d be better to use VB ?

  3. yvesdolc says:

    Anders already gave an answer on Look for "XML literals".