Displaying SVG images - c#

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)

Related

The input is not a valid Base-64 string exception when referencing an svg from another svg

I have a c# appliation which has an SVG parser in it . When I pass the follwing svg document (SVG01) to the parser it throws an exception :
The input is not a valid Base-64 string as it contains a non-base 64
character, more than two padding characters, or an illegal character
among the padding characters.
My svg that is going to the used in the application is as follows
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<svg width="210mm" height="297mm"
xmlns="http://www.w3.org/2000/svg" dominant-baseline="hanging">
<image x="2.34mm" y="3.44mm" height="20mm" width="40mm" href="D:\linkedSvg.svg"/>
</svg>
The LinkedSvg.svg is as follows
<svg xmlns="http://www.w3.org/2000/svg" dominant-baseline="hanging" width="400" height="110">
<rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)" />
</svg>
Looks like in SVG href needs to have some base:64 encoding characters right ?
IS there any other way to Link 2 svgs together ?

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 ?

Windows 8 - How To Change Live Tile Background Image?

How do I change the background image of a live tile in my Windows 8 app to a local image? The XML for the template I am using is:
<tile>
<visual>
<binding template="TileWideImageAndText01">
<image id="1" src="image1.png" alt="alt text"/>
<text id="1">Text Field 1</text>
</binding>
</visual>
</tile>
For the text, I use
XmlDocument xmltile= Windows.UI.Notifications.TileUpdateManager.GetTemplateContent(Windows.UI.Notifications.TileTemplateType.TileWideImageAndText01);
xmltile.GetElementsByTagName("text")[0].AppendChild(xmltile.CreateTextNode("73°F, Mostly Cloudy"));
TileNotification tileupdate = new Windows.UI.Notifications.TileNotification(xmltile);
Windows.UI.Notifications.TileUpdateManager.CreateTileUpdaterForApplication().Update(tileupdate);
But what about for the image?
From here:
The following code uses a local image from the app's package. This
type of image is included in your Visual Studio solution file and is
packaged as part of your app. These images are accessed by using the
"ms-appx:///" prefix. As a best practice, we also assign optional alt
text for accessibility purposes such as screen readers.
XmlNodeList tileImageAttributes = tileXml.GetElementsByTagName("image");
((XmlElement)tileImageAttributes[0]).SetAttribute("src", "ms-appx:///images/redWide.png");
((XmlElement)tileImageAttributes[0]).SetAttribute("alt", "red graphic");

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>

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.

Categories