Skip to content

Instantly share code, notes, and snippets.

@johnathan-sewell
Created November 29, 2012 14:24
Show Gist options
  • Save johnathan-sewell/4169425 to your computer and use it in GitHub Desktop.
Save johnathan-sewell/4169425 to your computer and use it in GitHub Desktop.
Example of saving XML to the Excel document file from a VSTO Excel Ribbon Addin
public void AddCustomXmlPartToWorkbook()
{
var workbook = Globals.ThisAddIn.Application.ActiveWorkbook;
const string xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<employees xmlns=\"http://myapi.com\">" +
"<employee>" +
"<name>Karina Leal</name>" +
"<hireDate>1999-04-01</hireDate>" +
"<title>Manager</title>" +
"</employee>" +
"</employees>";
Microsoft.Office.Core.CustomXMLPart employeeXMLPart = workbook.CustomXMLParts.Add(xmlString, System.Type.Missing);
}
public string RetrieveCustomXmlPartFromWorkbook()
{
var workbook = Globals.ThisAddIn.Application.ActiveWorkbook;
var retrievedXMLParts = workbook.CustomXMLParts.SelectByNamespace("http://myapi.com");
var customXMLPart = retrievedXMLParts.Cast<CustomXMLPart>().FirstOrDefault() ;
return customXMLPart != null ? customXMLPart.XML : String.Empty;
}
@johnathan-sewell
Copy link
Author

@johnathan-sewell
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment