Я разрабатываю приложение для управления XML в Windows Phone 8.1 RT. код ниже
private void btnXcute_tapped(object sender, TappedRoutedEventArgs e)
{
xmlmanipulation();
}
private async void xmlmanipulation()
{
Random rand = new Random();
try
{
var filex = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(@"xmlfile.xml");
XDocument xdoc = XDocument.Load(filex);
var word = xdoc.Descendants("word");
int max = word.Count();
txt1.Text = word.FirstOrDefault().Value.ToString();
txt1_Copy.Text = xdoc.Descendants("meaning").FirstOrDefault().Value.ToString();
//node removal part
xdoc.Root.Elements("wordset").Where(dim_word => dim_word.Element("word").Value == word.FirstOrDefault().Value.ToString() && dim_word.Element("meaning").Value == xdoc.Descendants("meaning").FirstOrDefault().Value.ToString())
.FirstOrDefault().Remove();
//file overwriting part
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("xmlfile.xml", CreationCollisionOption.ReplaceExisting);
using (var stream = await file.OpenStreamForWriteAsync())
{
xdoc.Save(stream); // Save XDocument into the stream
stream.Position = 0;
}
}
catch (Exception s)
{
txt1.Text = s.Message;
txt1_Copy.Text = s.Source;
}
}
Я поместил XML файл (xmlfile.xml) в основную папку проекта, как показано ниже
Но я получаю The system cannot find the file specified. (Exception from HRESULT:0x80070002)
The system cannot find the file specified. (Exception from HRESULT:0x80070002)
а источником Исключения является mscorlib
.
Я очень новичок в обработке XML. Пожалуйста, помогите мне. Все, что мне нужно для чтения XML файла, переделать на него первый элемент <wordset>
, сохранить файл (переписать) нажатием кнопки. У меня есть всего 4 элемента <wordset>
и если я <wordset>
кнопку 4 раза, все элементы будут удалены в файле.
Мой XML файл
<?xml version="1.0" encoding="utf-8" ?>
<xmlfile>
<wordset>
<word>word1</word>
<meaning>meaning1</meaning>
</wordset>
<wordset>
<word>word2</word>
<meaning>meaning2</meaning>
</wordset>
<wordset>
<word>word3</word>
<meaning>meaning3</meaning>
</wordset>
<wordset>
<word>word4</word>
<meaning>meaning4</meaning>
</wordset>
</xmlfile>
Вы пытаетесь получить доступ к файлу из Установленного местоположения, а не LocalFolder (в зависимости от действия сборки файла). Поэтому вы должны использовать:
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// not:
var folder = ApplicationData.Current.LocalFolder;
Вы также можете взглянуть на этот ответ.