简体   繁体   中英

How to pass xml file inside of code instead of a folder path?

I'am trying to not create a file, and pass xml document straight to a SkiaSharp method Load. I mean, is there the way to imitate a path? So here is the code:

public IActionResult svgToPng(string itemId, string mode = "
{
    var svgSrc = new XmlDocument();
    svgSrc.LoadXml(/*Some xml code*/);

    string svgSaveAs = "save file path";
    var quality = 100;

    var svg = new SkiaSharp.Extended.Svg.SKSvg();
    var pict = svg.Load(svgSrc); // HERE, it needs to be a path, not XmlDocument, but i want to pass straight

    var dimen = new SkiaSharp.SKSizeI
    (
        (int) Math.Ceiling(pict.CullRect.Width),
        (int) Math.Ceiling(pict.CullRect.Height)
    );

    var matrix = SKMatrix.MakeScale(1, 1);
    var img = SKImage.FromPicture(pict, dimen, matrix);

    // Convert to PNG
    var skdata = img.Encode(SkiaSharp.SKEncodedImageFormat.Png, quality);
    using(var stream = System.IO.File.OpenWrite(svgSaveAs))
    {
        skdata.SaveTo(stream);
    }

    ViewData["Content"] = "PNG file was created out of SVG.";

    return View(); 
}

The Load method seems to be this:

public SKPicture Load(
    using (var stream = File.OpenRead(filename))
    {
        return Load(stream);
    }
}

look at the code of that library :

https://github.com/mono/SkiaSharp.Extended/blob/master/SkiaSharp.Extended.Svg/source/SkiaSharp.Extended.Svg.Shared/SKSvg.cs

Look at the Load method, it has multiple implementations :

public SKPicture Load(string filename)
{
    using (var stream = File.OpenRead(filename))
    {
        return Load(stream);
    }
}

public SKPicture Load(Stream stream)
{
    using (var reader = XmlReader.Create(stream, xmlReaderSettings, CreateSvgXmlContext()))
    {
        return Load(reader);
    }
}

public SKPicture Load(XmlReader reader)
{
    return Load(XDocument.Load(reader));
}

You will need to pick one of them and use it. Now, nothing stops you from getting the code and adding one extra Load for an XML string for example, but since this is a library you do not control, I'd stick to what you are given.

You could use the XmlReader version, that's probably the closest one to what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM