Skip to content

Instantly share code, notes, and snippets.

@kkamegawa kkamegawa/xslxNamespace.cs
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.