Skip to content

Instantly share code, notes, and snippets.

@kkamegawa
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkamegawa/86c434111d8f493f30ab to your computer and use it in GitHub Desktop.
Save kkamegawa/86c434111d8f493f30ab to your computer and use it in GitHub Desktop.
Create OpenXML Spreadsheet's xml namespace in LINQ to XML.
using System.Xml.Linq;
namespace XslxNameSpaceSample
{
class Program
{
static void XNamespaceSample()
{
const string openXmlMarkupSchema = "http://schemas.openxmlformats.org/markup-compatibility/2006";
const string spreadSheetSchema = "http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac";
const string relationshipSchema = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
var sheetDocument = new XDocument();
sheetDocument.Declaration = new XDeclaration("1.0", "utf-8", "yes");
XNamespace xlsns = openXmlMarkupSchema;
XName mcIgnorableName = XName.Get("Ignorable", openXmlMarkupSchema);
XNamespace mcNameSpace = "mc";
XNamespace rNameSpace = "r";
XNamespace x14acNameSpace = "x14ac";
sheetDocument.Add(
new XElement("worksheet",
new XAttribute(XNamespace.Xmlns + "r", relationshipSchema),
new XAttribute(XNamespace.Xmlns + "x14ac", spreadSheetSchema)));
var x14acPrefix = sheetDocument.Document.Root.GetPrefixOfNamespace(XNamespace.Get(spreadSheetSchema));
var rPrefix = sheetDocument.Document.Root.GetPrefixOfNamespace(XNamespace.Get(relationshipSchema));
var rNamePrefix = sheetDocument.Document.Root.GetNamespaceOfPrefix("r");
var x14acNamePrefix = sheetDocument.Document.Root.GetNamespaceOfPrefix("x14ac");
sheetDocument.Document.Root.Add(new XAttribute(XNamespace.Xmlns + "mc", mcIgnorableName.NamespaceName));
sheetDocument.Document.Root.Add(new XAttribute(mcIgnorableName, x14acPrefix));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment