我正在用Visual Studio 2022制作一个UWP应用程序。用户可以将。xml和。xsl文件拖放到应用程序中,将它们转换成。pdf文件。
问题是Aspose既不能读取xml也不能读取xsl。他们试图通过拖放获取路径。我认为一个应用程序可以访问文件和目录,只要他们选择与文件拾取器或拖放,所以我有点困惑。
任何帮助都非常感谢!
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace XML2PDF
{
public sealed partial class MainPage : Page
{
string xmlPath;
string xslPath;
public MainPage()
{
this.InitializeComponent();
xmlPath = "";
xslPath = "";
}
private void BackgroundGrid_DragEnter(object sender, DragEventArgs e)
{
e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
}
private async void BackgroundGrid_Drop(object sender, DragEventArgs e)
{
if (e.DataView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.StorageItems))
{
var items = await e.DataView.GetStorageItemsAsync();
var filePaths = items.Select(x => x.Path).ToList();
foreach (var p in filePaths)
{
if (p.EndsWith(".xml"))
{
xmlPath = p;
continue;
}
if (p.EndsWith(".xsl"))
{
xslPath = p;
continue;
}
}
}
// TODO: check if xmlPath and xslPath aren't empty
this.ConvertXML2PDF(xmlPath, xslPath);
}
private async void ConvertXML2PDF(string xmlPath, string xslPath)
{
Aspose.Pdf.Document pdf = new Aspose.Pdf.Document();
string pdfPath = xmlPath.Replace(".xml", "pdf");
try
{
pdf.BindXml(xmlPath, xslPath);
// System.UnauthorizedAccessException: 'Access to the path 'C:Userspathtofilename.xsl' is denied.'
}
catch (System.Exception e)
{
throw e;
}
}
}
}
MainPage.xaml
<Page
x:Class="XML2PDF.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XML2PDF"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid x:Name="BackgroundGrid" Background="Transparent" AllowDrop="True" DragEnter="BackgroundGrid_DragEnter" Drop="BackgroundGrid_Drop" />
</Page>
Package.appmanifest
<?xml version="1.0" encoding="utf-8"?>
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
IgnorableNamespaces="uap mp">
...
<Capabilities>
<Capability Name="internetClient" />
</Capabilities>
</Package>
编辑
似乎是权限问题。我更新了ConvertXML2PDF
方法以读取从拖放返回的文件,并更新了Package.appxmanifest
以允许广泛访问。我把文件放在下载中。它抛出一个System.IO.FileNotFoundException
。现在很明显,应用程序无法访问该文件。
值得注意的是,Visual Studio说<rescap:Capability Name="broadFileSystemAccess" />
有无效的子元素命名空间,我不确定如何修复。
MainPage.xaml.cs
private async void ConvertXML2PDF(string xmlPath, string xslPath)
{
Aspose.Pdf.Document pdf = new Aspose.Pdf.Document();
string pdfPath = xmlPath.Replace(".xml", "pdf");
Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile sampleFile = await storageFolder.GetFileAsync(xslPath);
// System.IO.FileNotFoundException: The filename, directory name or volume label syntax is incorrect
}
Package.appxmanifest
<?xml version="1.0" encoding="utf-8"?>
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
...
<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
</Package>
UWP只允许通过Windows.Storage
使用文件…但是你可以把它转换成"normal"Stream
……所以只要Aspose
有一些Stream
API,那么你就回家了(看起来它有)…
将drop改为:
private async void BackgroundGrid_Drop(object sender, DragEventArgs e)
{
IStorageFile xmlFile = null;
IStorageFile xslFile = null;
if(e.DataView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.StorageItems))
{
var items = (await e.DataView.GetStorageItemsAsync()).OfType<IStorageFile>();
foreach(var item in items)
{
if(item.Path.EndsWith(".xml"))
{
xmlFile = item;
continue;
}
if(item.Path.EndsWith(".xsl"))
{
xslFile = item;
continue;
}
}
}
if(xmlFile != null && xslFile != null)
{
ConvertXML2PDF(xmlFile, xslFile);
}
}
和ConvertXML2PDF方法到:
Aspose.Pdf.Document pdf = new Aspose.Pdf.Document();
try
{
using(var xmlStream = await xmlFile.OpenStreamForReadAsync())
{
using(var xslStream = await xslFile.OpenStreamForReadAsync())
{
pdf.BindXml(xmlStream, xslStream);
//C:UsersUserAppDataLocalPackagessome_guid_6vqfjjxsnvvwrLocalState
//var pdfFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(xmlFile.Name.Replace("xml", "pdf"), CreationCollisionOption.ReplaceExisting);
//or ask user
var picker = new FileSavePicker();
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
picker.FileTypeChoices.Add("PDF", new[] { ".pdf" });
var pdfFile = await picker.PickSaveFileAsync();
using(var pdfStream = await pdfFile.OpenStreamForWriteAsync())
{
pdf.Save(pdfStream);
}
}
}
}
catch(System.Exception e)
{
throw e;
}
}
你应该可以走了