Use XSLT and C #. Net to convert SVG to XAML

Source: Internet
Author: User

I found an open source code named SVG-Convert-Driver-XAML-0.02 on the internet, after decompression found that it is not written using. Net code, so it is not suitable for my needs.

However, there is a file that is very useful, that is: extract the directory/SVG-Convert-Driver-XAML-0.02/lib/SVG/convert/driver/XAML/svg2xaml. XSL. Because the XSLT file also references a file named colors. XML, this file is not included in the compressed package provided by the original author. As a result, I had to infer the structure of colors. XML based on the XPath in this XSLT. According to line 71st of svg2xaml. XSL:
<XSL: variable name = "named_color_hex" select = "document ('colors. XML ')/colors/color [@ name = translate ($ colorspec, 'abcdefghijklmnopqrstuvwxy', 'abcdefghijklmnopqrstuvwxy')]/@ hex "/>
After analysis, I found that the structure of colors. XML is roughly as follows:
<Colors>
<Color name = "aliceblue" hex = "f0f8ff".../>
<! -- More color elements and their attributes -->
</Colors>

I want to use it to convert SVG to XAML.

First, let's take a look at the final run after writing the complete program:

Procedure:

So I searched for colors. xml on the Internet and found a similar file. Its structure is:
<? XML version = "1.0" encoding = "UTF-8"?>
<Colors>
<Color name = "aliceblue" hex = "f0f8ff" HSL = "240, 240,248,255" RGB = ""/>
<Color name = "antiquewhite" hex = "faebd7" HSL = "235, 250,235,215" RGB = ""/>
<Color name = "Aqua" hex = "00 FFFF" HSL = "180,240,240" inversename = "red" RGB = "0,255,255"/>
<Color name = "aquamarine" hex = "7fffd4" HSL = "160,120,240" RGB = "127,255,212"/>
<! -- More color elements -->
<Color name = "yellowgreen" hex = "9acd32" HSL = "80,181,193" RGB = "154,205, 50"/>
</Colors>

The rest of the work is to open the SVG file in the pop-up dialog box, and then use unzip compiledtransform, javastsettings, xmlreadersettings to convert the file to XAML.
Openfiledialog ofd = new openfiledialog ();
Ofd. Filter = "*. SVG | *. SVG ";
Ofd. showdialog ();
If (OFD. filename! = String. Empty)
{
Stringwriter Sw = new stringwriter ();
Xmlwriter = new xmltextwriter (SW );
Try
{
Using (streamreader = new streamreader (OFD. filename ))
{
Txtboxsvgcode. Text = streamreader. readtoend ();
}
}
Catch (exception exc)
{
System. Windows. MessageBox. Show (string. Format ("error, error:/R/n {0}", exc. Message ));
}

String untrusteduri = @ "http://www.anuntrustedsite.com/test.xsl ";
XmlresolverResolver = new xmlsecureresolver (New xmlurlresolver (), untrusteduri );
XsltsettingsSettings = effectsettings. trustedxslt;
Settings. enabledocumentfunction = true;
XslcompiledtransformTransform = new transform compiledtransform ();
Transform. Load (@ "../svg2xaml. XSL", settings, new xmlurlresolver ());
XmlreadersettingsRs = new xmlreadersettings ();
Rs. prohibitdtd = false;
Transform. Transform (xmlreader. Create (Ofd. filename, RS), null, xmlwriter, resolver );
Xmlwriter. Close ();
// The code for displaying the final result graph (omitted)
}

During debugging, it was found that the above program can perform normal conversion for some SVG, but some cannot be converted, and an exception is thrown. For example, an exception occurs in line 71st at svg2xaml. XSL. However, this XSLT file has been detected many times and there is no format problem. The conversion speed of some SVG files is also very slow, especially when path embedding exists-preliminary estimation is related to some algorithms in the XSLT template.

If you have time, look for the relevant reasons. Here I will focus on the Conversion Method and hope to enlighten you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.