PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Derinsis.B2B/Helpers/Html/GlobalStatics.cs

https://bitbucket.org/tbagis/derinsis-b2b
C# | 60 lines | 55 code | 5 blank | 0 comment | 6 complexity | bd4a7583ecf214ba36fbfdc037e82e81 MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Web;
 5using System.Drawing;
 6using System.IO;
 7using System.Linq;
 8using System.Text;
 9using System.Xml;
10using System.Xml.Linq;
11using System.Xml.Xsl;
12
13namespace DerinSIS.B2B.Helpers.Html
14{
15    public static class GlobalStatics
16    {
17        private static XNamespace cac = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2";
18        private static XNamespace cbc = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2";
19        public static string FaturaHtmlView(this XDocument doc, bool hata = false)
20        {
21            var xslDocumentStr = GetXsltForEFatura(doc);
22            var xslTransform = new XslCompiledTransform();
23            xslTransform.Load(XmlReader.Create(new StringReader(xslDocumentStr)));
24            var result = new StringBuilder();
25            using (var sw = new StringWriter(result))
26            {
27                using (var xw = XmlWriter.Create(sw))
28                {
29                    xslTransform.Transform(doc.CreateReader(), null, xw);
30                }
31            }
32
33            return result.ToString();
34        }
35        private static string GetXsltForEFatura(XDocument doc)
36        {
37            var faturaNo = doc.Root.Element(cbc + "ID").Value;
38            var xlstDocElem = doc.Root.Elements()
39                .Where(x => x.Name == cac + "AdditionalDocumentReference")
40                .Where(x => x.Element(cac + "Attachment") != null)
41                .Select(x => x.Element(cac + "Attachment"))
42                .Where(x => x.Element(cbc + "EmbeddedDocumentBinaryObject") != null)
43                .Select(x => x.Element(cbc + "EmbeddedDocumentBinaryObject"))
44                .Where(x => x.Attribute("filename") != null)
45                .Where(x => x.Attribute("filename").Value.EndsWith(".xslt"))
46                .Where(x => x.Attribute("filename").Value.Contains(faturaNo))
47                .FirstOrDefault();
48
49            var embbeddedXsltBytes = Convert.FromBase64String(xlstDocElem.Value);
50            var xslTransformtest = new XslCompiledTransform();
51
52            var s1 = Encoding.UTF8.GetString(embbeddedXsltBytes);
53            if (Encoding.ASCII.GetBytes(s1[0].ToString())[0].ToString() == "63")
54                s1 = s1.Substring(1, s1.Length - 1);
55            xslTransformtest.Load(XmlReader.Create(new StringReader(s1)));
56
57            return s1;
58        }
59    }
60}