将BlobStorage中的Blob加载到XslCompiledTransform.Load()中

时间:2018-06-05 09:26:12

标签: azure azure-storage-blobs

我在AzureBlobStorage中有一个XSLT文件。

我需要将该Blob加载到XslCompiledTransform.Load()中。

function clickLogin() {
    var szIP = $("#loginip").val(),
    szPort = $("#port").val(),
    szUsername = $("#username").val(),
    szPassword = $("#password").val();

    if ("" == szIP || "" == szPort) {
       return;
    }

    var iRet = WebVideoCtrl.I_Login(szIP, 1, szPort, szUsername, szPassword, {
        success: function (xmlDoc) {
        showOPInfo(szIP + " login success!");

            $("#ip").prepend("<option value='" + szIP + "'>" + szIP + "</option>");
            setTimeout(function () {
                $("#ip").val(szIP);
                getChannelInfo();
            }, 10);
        },
        error: function () {
            showOPInfo(szIP + " login failed!");
        }
    });

    if (-1 == iRet) {
        showOPInfo(szIP + " login already !");
    }
}

我曾尝试使用MemoryStream和StreamReader等。

请帮忙...

2 个答案:

答案 0 :(得分:1)

此答案将适用于您的问题

        CloudBlobClient blobClient = GetCloudBlobContainer();
        CloudBlob blob_xslt = blobClient.GetContainerReference("container1").GetBlobReference("name.xslt");

        CloudBlob blob_xml = blobClient.GetContainerReference("container2").GetBlobReference(name.xml);

        StringBuilder stringBuilder = new StringBuilder();
        XslCompiledTransform xslt = new XslCompiledTransform();
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        settings.ConformanceLevel = ConformanceLevel.Fragment;
        settings.CloseOutput = false;
        string xml_string = null;
        string xslt_string = null;
        using (var xmlstream = await blob_xml.OpenReadAsync())
        {
            using (var xsltstream = await blob_xslt.OpenReadAsync())
            {

                StreamReader xmlreader = new StreamReader(xmlstream);
                StreamReader xsltreader = new StreamReader(xsltstream);
                while (!xmlreader.EndOfStream && !xsltreader.EndOfStream)
                {
                    xml_string = xmlreader.ReadToEnd();
                    xslt_string = xsltreader.ReadToEnd();
                }
                byte[] byteArray = Encoding.UTF8.GetBytes(xslt_string);
                var tmpFile = Path.GetTempFileName();
                var tmpFileStream = System.IO.File.OpenWrite(tmpFile);
                tmpFileStream.Write(byteArray, 0, byteArray.Length);
                tmpFileStream.Close();
                xslt.Load(tmpFile);

                byte[] byteArray1 = Encoding.UTF8.GetBytes(xml_string);
                var tmpFile1 = Path.GetTempFileName();
                var tmpFileStream1 = System.IO.File.OpenWrite(tmpFile1);
                tmpFileStream1.Write(byteArray1, 0, byteArray1.Length);
                tmpFileStream1.Close();

                xslt.Transform(tmpFile1, XmlWriter.Create(stringBuilder, settings));

            }

        } 

答案 1 :(得分:0)

我试过MemoryStream并且它有效。请参阅下面的代码。

var stream_xslt = new MemoryStream();
await blob_xslt.DownloadToStreamAsync(stream_xslt);
stream_xslt.Position = 0;

var stream_xml = new MemoryStream();
await blob_xml.DownloadToStreamAsync(stream_xml);
stream_xml.Position = 0;

XslCompiledTransform xslt = new XslCompiledTransform();
StringBuilder stringBuilder = new StringBuilder();
xslt.Load(XmlReader.Create(stream_xslt));
xslt.Transform(XmlReader.Create(stream_xml), 
XmlWriter.Create(stringBuilder));

要使用uri访问blob,如果其访问级别不公开,则必须使用sas uri。由于您可以连接到存储帐户,因此如果确实有效,则可以将blob下载到流中。