Skip to content

Commit

Permalink
support saving XML to XPath instead of XML root
Browse files Browse the repository at this point in the history
  • Loading branch information
christso committed Jul 10, 2016
1 parent 17fa8f7 commit adde6bb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
3 changes: 2 additions & 1 deletion DaxDrill/AddIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using DG2NTT.DaxDrill.Logic;
using DG2NTT.DaxDrill.DaxHelpers;
using DG2NTT.DaxDrill.Helpers;
using System.Collections;
using Office = Microsoft.Office.Core;

namespace DG2NTT.DaxDrill
{
Expand Down Expand Up @@ -161,7 +163,6 @@ public static void About()
{
try
{

AboutBox.ShowForm();
}
catch (Exception ex)
Expand Down
18 changes: 17 additions & 1 deletion DaxDrill/ExcelHelpers/ExcelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static List<string> ReadCustomXmlParts(Excel.Workbook workbook)
return result;
}

public static string ReadCustomXmlPart(Excel.Workbook workbook, string xNameSpace,
public static string ReadCustomXmlNode(Excel.Workbook workbook, string xNameSpace,
string xPath)
{
Office.CustomXMLNode node = GetCustomXmlNode(workbook, xNameSpace, xPath);
Expand All @@ -97,11 +97,13 @@ public static Office.CustomXMLNode GetCustomXmlNode(Excel.Workbook workbook, str
Office.CustomXMLParts ps = workbook.CustomXMLParts;
ps = ps.SelectByNamespace(xNameSpace);


for (int i = 1; i <= ps.Count; i++)
{
Office.CustomXMLPart p = ps[i];
var nsmgr = p.NamespaceManager;
nsmgr.AddNamespace("x", xNameSpace);

Office.CustomXMLNode node = p.SelectSingleNode(xPath);
if (node != null)
return node;
Expand All @@ -125,6 +127,20 @@ public static void UpdateCustomXmlPart(Excel.Workbook workbook, string namespace
AddCustomXmlPart(workbook, namespaceName, xmlString);
}

public static void UpdateCustomXmlNode(Excel.Workbook workbook, string namespaceName, string xmlString, string xPath)
{
Office.CustomXMLParts ps = workbook.CustomXMLParts.SelectByNamespace(namespaceName);

foreach (Office.CustomXMLPart p in ps)
{
var nsmgr = p.NamespaceManager;
nsmgr.AddNamespace("x", namespaceName);

Office.CustomXMLNode oldNode = p.SelectSingleNode(xPath);
oldNode.ParentNode.ReplaceChildSubtree(xmlString, oldNode);
}
}

public static void DeleteCustomXmlPart(Excel.Workbook workbook, string namespaceName)
{
IEnumerator e = workbook.CustomXMLParts.GetEnumerator();
Expand Down
2 changes: 1 addition & 1 deletion DaxDrill/Logic/QueryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static IEnumerable<DetailColumn> GetCustomDetailColumns(Excel.Range rngCe

Measure measure = QueryClient.GetMeasure(rngCell);

string xmlString = ExcelHelper.ReadCustomXmlPart(
string xmlString = ExcelHelper.ReadCustomXmlNode(
workbook, Constants.DaxDrillXmlSchemaSpace,
Constants.TableXpath);
List<DetailColumn> columns = DaxDrillConfig.GetColumnsFromTableXml(Constants.DaxDrillXmlSchemaSpace, xmlString, wbcnn.Name, measure.Table.Name);
Expand Down
7 changes: 5 additions & 2 deletions DaxDrill/Logic/XmlEditController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ public void LoadXmlFromWorkbook()
{
Excel.Workbook workbook = null;
workbook = ExcelHelper.FindWorkbook(xmlEditForm.WorkbookText);
string xmlString = ExcelHelper.ReadCustomXmlPart(workbook, xmlEditForm.NamespaceText, xmlEditForm.XpathText);
string xmlString = ExcelHelper.ReadCustomXmlNode(workbook, xmlEditForm.NamespaceText, xmlEditForm.XpathText);
xmlEditForm.XmlText = xmlString;
}

public void SaveXmlToWorkbook()
{
Excel.Workbook workbook = ExcelHelper.FindWorkbook(xmlEditForm.WorkbookText);
ExcelHelper.UpdateCustomXmlPart(workbook, xmlEditForm.NamespaceText, xmlEditForm.XmlText);
if (xmlEditForm.XpathText == ".." || string.IsNullOrWhiteSpace(xmlEditForm.XpathText))
ExcelHelper.UpdateCustomXmlPart(workbook, xmlEditForm.NamespaceText, xmlEditForm.XmlText);
else
ExcelHelper.UpdateCustomXmlNode(workbook, xmlEditForm.NamespaceText, xmlEditForm.XmlText, xmlEditForm.XpathText);
}
}
}
2 changes: 0 additions & 2 deletions DaxDrill/UI/XmlEditForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,11 @@ public void RefreshWorkbooksControl()
{
Excel.Application excelApp = (Excel.Application)ExcelDnaUtil.Application;

Excel.Workbook workbook = excelApp.ActiveWorkbook;
var wbList = ExcelHelper.ListWorkbooks(excelApp);
cbWorkbooks.Items.Clear();
cbWorkbooks.Items.AddRange(wbList.ToArray());
}


#endregion

#region Form Events
Expand Down

0 comments on commit adde6bb

Please sign in to comment.