在UWP應用程序,你可以使用XmlDocument class檢索和創建所有XML對象。
我假設你的列表在XML文件中提供了子節點(XMLElement),而不是在一個節點(XMLText)中的文本,然後例如你可以在XML中創建一個節點列表並將該文件保存到應用程序的這樣的漫遊文件:
private async void Create_File(object sender, RoutedEventArgs e)
{
XmlDocument xdoc = new XmlDocument();
XmlElement root = xdoc.CreateElement("List");
xdoc.AppendChild(root);
for (int i = 0; i < 100; i++)
{
XmlElement item = xdoc.CreateElement("item" + i);
root.AppendChild(item);
}
XmlElement books = xdoc.CreateElement("Books");
StorageFolder roamingFolder = Windows.Storage.ApplicationData.Current.RoamingFolder;
StorageFile xmlFile = await roamingFolder.CreateFileAsync("XMLList.xml", CreationCollisionOption.ReplaceExisting);
await xdoc.SaveToFileAsync(xmlFile);
}
此代碼創建XML文件中的「根」,在這「根」也有100個項目節點,每個項目被命名爲"Item" + i
;
然後,我們需要閱讀這些內容爲ListView
這樣的:
private ObservableCollection<MyList> mylist = new ObservableCollection<MyList>();
private async void Read_Data_Root(object sender, RoutedEventArgs e)
{
StorageFolder roamingFolder = Windows.Storage.ApplicationData.Current.RoamingFolder;
StorageFile xmlFile = await roamingFolder.GetFileAsync("XMLList.xml");
XmlDocument xdoc = await XmlDocument.LoadFromFileAsync(xmlFile);
txt.Text = xdoc.GetXml(); //show the XML representation of the node and all its descendants in a textblock.
XmlElement root = xdoc.DocumentElement;
mylist.Clear();
foreach (var item in root.ChildNodes)
{
mylist.Add(new MyList { xmlitem = item.GetXml() });
} //show all items in a listview
}
類MyList
是因爲這很簡單:
public class MyList
{
public string xmlitem { get; set; }
}
我想,也許你已經完成了所有工作以上,然後我刪除項目通過ListView
的SelectionChanged event並確認刪除項目在Flyout
,這裏是我的ListView
:
<ListView x:Name="listView" Grid.Row="0" SelectionChanged="Show_Delete_Button" ItemsSource="{x:Bind mylist}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding xmlitem}" />
</DataTemplate>
</ListView.ItemTemplate>
<FlyoutBase.AttachedFlyout>
<Flyout>
<Button Content="Delete this item" Click="Delete_Data" />
</Flyout>
</FlyoutBase.AttachedFlyout>
</ListView>
以及用於刪除數據的代碼:
private int selectedindex;
private void Show_Delete_Button(object sender, SelectionChangedEventArgs e)
{
FlyoutBase.ShowAttachedFlyout(sender as ListView);
selectedindex = listView.SelectedIndex;
}
private async void Delete_Data(object sender, RoutedEventArgs e)
{
var item = mylist.ElementAt(selectedindex);
mylist.RemoveAt(selectedindex); //remove from listview
StorageFolder roamingFolder = Windows.Storage.ApplicationData.Current.RoamingFolder;
StorageFile xmlFile = await roamingFolder.GetFileAsync("XMLList.xml");
XmlDocument xdoc = await XmlDocument.LoadFromFileAsync(xmlFile);
XmlElement root = xdoc.DocumentElement;
string itemstring = item.xmlitem.TrimStart('<');
itemstring = itemstring.TrimEnd('>', '/');
root.RemoveChild(root.GetElementsByTagName(itemstring).FirstOrDefault()); //remove from root element.
await xdoc.SaveToFileAsync(xmlFile); //save the new list
}
這是從刪除XML文件的節點列表中的代碼。如果您以不同的方式在XML文件中創建列表並構建,請張貼您的代碼。順便說一句,在這裏我們使用命名空間「Windows.Data.Xml.Dom」,而不是命名空間「System.Xml」。