Silverlight XML Save does not overwrite the entire file - c#

The best way to explain my problem is by a example.
I have a Xml file with some elements in it. When I remove a element, I save the file again and this problem occurs:
Before save
<?xml version="1.0" encoding="utf-8"?>
<ElementTree>
<Elements1 />
<Elements2>
<Element Name="TestElement" ID="4efa7cc9-a89a-429b-81f4-b00cde729f24" />
</Elements2>
<Elements3 />
</ElementTree>
After save
<?xml version="1.0" encoding="utf-8"?>
<ElementTree>
<Elements1 />
<Elements2 />
<Elements3 />
</ElementTree>D="4efa7cc9-a89a-429b-81f4-b00cde729f24" />
</Elements2>
<Elements3 />
</ElementTree>
What I suspect it is doing: It keeps writing the text untill it has reached the end of the text to be saved and then it stops. It does not remove the text that is still writen in the file AFTER the last character in the stream.
How can I fix this?

Make sure you replace the existing file when opening it and you close the stream correctly.

Related

Change xml element outer text c#

Hi I have a xml file that contains
<?xml version="1.0" encoding="utf-8"?>
<resources>
<iconback img1="iconback" />
<iconmask img1="iconmask" />
<iconupon img1="iconupon" />
<scale factor="0.7" />
</resources>
How can I change and save the img="iconback" outer text and Scale factor="0.7?
Everything I try fails with
Cannot set a value on node type 'Element'

EPPlus - Update Custom Ribbon Elements

I have a excel file which contains several items in a custom ribbon. I've added these custom elements with Microsoft's "Custom UI Editor for Microsoft Office", my customization looks something like the following:
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon>
<tabs>
<tab id="XXX" label="XXX" insertAfterMso="TabInsert">
<group id="YYY" label="YYY">
<button id="btnXXX"
label="XXXX"
imageMso="PivotTableNewStyle"
size="large"
onAction="ZZZZ"
screentip="XXX"
supertip="XXX" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
I would like to modify(Add remove elements, change elements) this header with EPPlus, is this possible? If so how would it be done?
I have not found a way to do this in EPPlus, but I have found a way to edit it with the "DocumentFormat.OpenXml" library. Specifically to access that portion of the file I use the following code:
((SpreadsheetDocument)document.WorkbookPart.OpenXmlPackage).RibbonAndBackstageCustomizationsPart
I'm still not sure if it can be done in EPPlus. Maybe in a future version...

How can one set minimalist interface of Excel with C#?

Using C#, I'm writing a program that has to use Excel only in order to edit some tabular data provided by my program. My program just creates an Excel application (the Microsoft.Office.Interop.Excel.Application object), creates within it a workbook, creates a worksheet, puts the data in Excel, and, after this, allows to a user to edit these data. The edited data can be easily read by my program. That's, in principal, all what I need from Excel.
This, therefore, means that a user of my program should not be able to format data in Excel, save an Excel file to the hard drive, open Excel files, and so on and so on. A user even does not need the Excel ribbon. Working with formulas is also does not required. Hotkeys (like "Ctrl + B" making the font bold, "Ctrl + S" saving the file, and so on) should be also not workable.
My question is: Is there a way to simply set such a minimalist interface for Excel? Or I should disable any aspect mentioned above by hand, step by step (disable the ribbon, switch off the hotkeys ans so on)?
When working within a VSTO project, you can disable most/all of the UI by using a custom ribbon XML file (there is a visual designer too but I doubt it would be sufficient for this).
To remove things, add a section for what you're removing, and set visible="false" or enabled="false" (users could still potentially add it back to the ribbon, but disabling it makes it grayed out).
e.g.:
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
<commands>
<command idMso="Cut" enabled="false" />
</commands>
<ribbon>
<tabs>
<tab idMso="TabHome" visible="false" />
</tabs>
</ribbon>
<contextualTabs>
<tabSet idMso="TabSetDrawingTools" visible="false" />
</contextualTabs>
<backstage>
<tab idMso="TabInfo" visible="false">
</backstage>
<contextMenus>
<contextMenu idMso="ContextMenuText">
<!-- you can't hide a context menu entirely, you have to hide each item -->
<button idMso="FontDialog" visible="false" />
</contextMenu>
</contextMenus>
</customUI>
This may also be possible if you're working with Office in the way that you seem to be, I don't really know.

XmlReader.WriteRaw is still escaping '<' and '>'

I'm definitely missing something. I have an XML file that stores HTML snippets, as below:
<?xml version="1.0" encoding="utf-8"?>
<presentation>
<slide id="1" title="Test Slide 1">
<h1>This is test slide 1.</h1>
<p>Information on test slide.</p>
</slide>
<slide id="2" title="Test Slide 2">
<h2> This is test slide 2.</h2>
<p>Information on test slide.</p>
</slide>
<slide id="3" title="Test Slide 3">
<h3> This is test slide 3.</h3>
<p>Information on test slide.</p>
</slide>
</presentation>
I'm building a ASP.net form that allows me to edit the contents of the "slides" and then save my changes. In order to do that, first i find the appropriate slide by id. Then I remove all the contents of the slide with RemoveNodes(), and then attempt to write the new contents with WriteRaw(contents).
XDocument xmlDoc = XDocument.Load(filepath);
XElement slide = xmlDoc.Descendants("slide").First(el => el.Attribute("id").Value.Equals(id));
slide.RemoveNodes();
using (var writer = slide.CreateWriter())
{
// contents are retrieved from a TextBox
writer.WriteRaw(contents);
}
xmlDoc.Save(filepath);
The code runs without error. However, when I check the xml file, the < and > characters for the html tags have been converted to < and >.
...
<slide id="1" title="Test Slide 1"><h1>This is test slide 1.</h1>
<p>Information on test slide.</p>></slide>
...
I'm guessing that another object is escaping the characters in the background. SaveOptions.DisableFormatting doesn't change the behavior. Can anyone point me in the right direction?
Sorry, but XmlWriter.WriteRaw works on XmlWriters writing to a stream or file but not when populating objects in System.Xml or System.Xml.Linq.
Use slide.Add(XElement.Parse("<dummy>" + contents + "</dummy>").Nodes()) instead.

How to enable configSource attribute for Custom Configuration Section in .NET?

following the wealth of information found here how can we get an external .config to work? I've tried the same setup I would use for an external appSettings file, but it is unable to find the file for my custom section.
<configSections>
...
<section name="CustomSettings" type="Fully.Qualified.TypeName.CustomSettings, AssemblyName" />
</configSections>
<!-- this works -->
<CustomSettings attrib1="val1" attrib2="val2" .../>
however...
<!--this does not work-->
<CustomSettings configSource="someExternalFile.config"/>
where someExternalFile.config would contain
<CustomSettings attrib1="val1" attrib2="val2" .../>
any ideas?
The actual file, must be placed relative to the project output folder (by default "\bin\debug" or "bin\Release"
Also, The file in your project tree, look at the properties of the file, and make sure the "Copy to Output Directory" setting is set to "Copy Always" or "Copy if Newer"
EDIT: make sure the separate config file has an xml Element header. The entire file contents should read as follows:
<?xml version="1.0" encoding="utf-8" ?>
<CustomSettings attrib1="val1" attrib2="val2" .../>

Categories