XmlReader.WriteRaw is still escaping '<' and '>' - c#

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.

Related

Error : The template's contents do not follow the schema

I have a SVG document defined as below
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<svg width="210mm" height="297mm" xmlns="http://www.w3.org/2000/svg" dominant-baseline="hanging">
<text x="108.18mm" y="7.14mm" font-size="12pt" font-family="Times New Roman" font-weight="bold" font-style="italic">SVG Template to Test Various Styling</text>
<style>
text {
fill: gold;
stroke: maroon;
stroke-width: 2px;
}
</style>
</svg>
While this SVG gets Previewed in the browser as expected when I pass this on to a C# appilcation which is built to read svgs if throws an exception saying **`
The template's contents do not follow the schema
`** .
I have no Idea as to what is causing this as the browser seems to be rendering this as expected
Further the error also says :- The element 'svg' in namespace 'http://www.w3.org/2000/svg' has invalid child element 'style' in namespace 'http://www.w3.org/2000/svg'
Any idea that may help me ?

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...

Displaying SVG images

I have some SVG files stored in a database as a string. What would be the best way to display these images on a razor view template?
I was hoping to just display the string directly
something like
<div>
#Model.Icon
</div>
but when I do this quotation marks are placed around the content so the image doesnt display
example svg format is
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 21.0.2, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 127.4 72.2" style="enable-background:new 0 0 127.4 72.2;" xml:space="preserve">
<title>Artboard 2 copy 11</title>
<path d="M126.3,7v10.5c1.3,7.7,1.5,18.2,0.2,29.7c-0.9,8.5-2.6,16.8-5.1,24.9h-7l-4.7,0h-8.7c-1,0-1.8-1.4-1.8-3.1l0,0
c0-1.7,0.8-3.1,1.8-3.1h1.8c1.2-4.8,2.1-9.6,2.8-14.5h-1.9c-2.8-0.2-5.1-2.5-5.1-5.3l-0.1-11.8H87.1c-9.7,0-19.8-3.3-22.5-11.3h-7
c-0.6,0-1-0.5-1-1.1v-0.6H44.9v-3.6h-5.4c-1.2-2.6-3.3-3.1-7.1-3.7c-0.1,0,4.1,1.4,4,3.7h-6.1c-1.2-2.6-3.4-3.1-7.1-3.8
c-0.1,0,4.1,1.4,4,3.7h-6.1c-1.2-2.6-3.4-3.1-7.1-3.7c-0.1,0,4.1,1.4,4,3.7h-5.6c-1.2-2.6-3.4-3.1-7.1-3.7c-0.1,0,4.1,1.4,4,3.7H4.4
c-0.9-1.8-2.2-2.6-4.4-3.2v-1.3h44.9V9.6h11.7V9c0-0.6,0.4-1.1,1-1.1h6.5C67.3-3.2,126.3-1.8,126.3,7z M106.1,48.7
c0.1-0.5,0.1-0.9,0.2-1.4c0.5-4.5,0.8-8.8,0.8-12.9h-5.6l0.1,11.8c0,1.1,0.8,2.1,1.8,2.4c0.2,0,0.4,0.1,0.5,0.1H106.1z"/>
</svg>
you can use :
#Html.Raw(Model.Icon)

AvalonEdit (C#) Custom attributes in XSHD files

I'm working on highlighting a small scripting language (that is not a derivative of any of the built in's) and I had a custom XML file setup for different syntax keywords and such.
I would really like to use the XSHD system that exists in AvalonEdit for loading custom keywords, but I need to have custom attributes in them.
So basically, after a lot of searching through the source code, I could not really pin-point the file that reads and specifies the keywords and rulesets.
Does anyone here know where I can edit the needed class to read some custom attributes and storing them?
I have all the XSHD and higlighting stuff in AvalonEdit down and working, so it's not using it that is the problem, per say, it's more the problem of using custom attributes in the keywords collection of the XSHD file.
In example:
<Keywords>
<Word defaultValue="hello" requiresShader="shadertype1, shadertype2" someCustomAttr="value">wordname</word>
</Keyword>
So, I need to set some custom properties to the parser of the XSHD file to load these custom attributes and store them in a custom class I have made that handles a few logical components that are crucial to what I'm trying to do.
I already have a working parser that parses a XML file of all my keywords and their properties (but I wrote it to ScintillaNET, but found AvalonEdit afterwards, that works better with WPF and such).
I will edit the original post with this information, sorry about possible misinformation.
Thank you
To load an XSHD file, you need to import the following libraries:
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
And then wherever you create the text editor (omit the first line and just reference the TextEditor if you create it in XAML), use the following code to load def.xshd.
// TextEditor is an AvalonEdit.TextEditor
TextEditor edit = new TextEditor();
XmlReader reader = XmlReader.Create("def.xshd");
edit.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
A good sample xshd file that I am using for a language in the works is this:
<SyntaxDefinition name="XAPL"
xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<Color name="Comment" foreground="Green" />
<Color name="String" foreground="Pink" />
<Color name="Xml" foreground="Orange" />
<!-- This is the main ruleset. -->
<RuleSet>
<Span color="Comment" begin="//" />
<Span color="Comment" multiline="true"
begin="/\*" end="\*/" />
<Span color="String">
<Begin>"</Begin>
<End>"</End>
<RuleSet>
<!-- nested span for escape sequences -->
<Span begin="\\" end="." />
</RuleSet>
</Span>
<Span color="Xml" multiline="false">
<Begin>spaz</Begin>
<End>spaz</End>
</Span>
<Keywords fontWeight="bold" foreground="Blue">
<Word>dealwith</Word>
<Word>import</Word>
<Word>end</Word>
<Word>var</Word>
<Word>default</Word>
<Word>template</Word>
<Word>sub</Word>
<Word>category</Word>
<Word>if</Word>
<Word>otherwise</Word>
<Word>and</Word>
<Word>xor</Word>
<Word>string</Word>
<Word>int</Word>
<Word>convert</Word>
<Word>to</Word>
<Word>escape</Word>
<Word>native</Word>
<Word>loop</Word>
<Word>is</Word>
<Word>to</Word>
<Word>from</Word>
<Word>by</Word>
</Keywords>
<!-- Digits -->
<Rule foreground="DarkGray">
\b0[xX][0-9a-fA-F]+ # hex number
| \b
( \d+(\.[0-9]+)? #number with optional floating point
| \.[0-9]+ #or just starting with floating point
)
([eE][+-]?[0-9]+)? # optional exponent
</Rule>
</RuleSet>
</SyntaxDefinition>

Silverlight XML Save does not overwrite the entire file

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.

Categories