async public void readXmlFile() { //XmlDocument doc = new XmlDocument(); //doc.LoadXml("ms-appx:///NewFolder/XMLFile.xml"); //XmlElement rootElem = doc.DocumentElement; //XmlNodeList personNodes = rootElem.GetElementsByTagName("person"); //StorageFolder folder1 = null; // StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/city.plist")); // ////StorageFolder localFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; //foreach (StorageFolder folder in await localFolder.GetFoldersAsync()) //{ // if (folder.DisplayName == "Citys") // { // folder1 = folder; // break; // } //} //foreach (StorageFile file in await folder1.GetFilesAsync()) //{ // if (file.DisplayName == "city.plist") // { // file1 = file; // break; // } //} StorageFile file1 = null; string text; Citys city; List<Citys> citys = new List<Citys>(); var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("NewFolder"); foreach (StorageFile file in await folder.GetFilesAsync()) { if (file.Name == "XMLFile.xml") { file1 = file; break; } } if (file1 == null) return; IRandomAccessStream accesssStream = await file1.OpenReadAsync(); //string fileContent =await FileIO.ReadTextAsync(file1); using (StreamReader stream = new StreamReader(accesssStream.AsStreamForRead((int)accesssStream.Size))) { text = stream.ReadToEnd(); // DataContractSerializer serializer = new DataContractSerializer(typeof(Citys)); //city = (Citys)serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(text)) ); // city = (Citys)serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(text))); } string a = "<string>"; string b = "</string>"; int indexa=1; int indexb=1; for (int i = 0; i < text.Length; i++) { indexa = text.IndexOf(a, indexa + 1); indexb = text.IndexOf(b, indexb + 1); if (indexa == -1 && indexb == -1) break; string s = text.Substring(indexa+a.Length , indexb - indexa-a.Length ); city = new Citys(); city.name = s; citys.Add(city); } citysName.ItemsSource = citys; //using (IInputStream inStream = await file1.OpenSequentialReadAsync()) //{ // DataContractSerializer serializer = new DataContractSerializer(typeof(Citys)); // city = (Citys)serializer.ReadObject(inStream.AsStreamForRead()); //} }
async public void readXmlFile() { //XmlDocument doc = new XmlDocument(); //doc.LoadXml("ms-appx:///NewFolder/XMLFile.xml"); //XmlElement rootElem = doc.DocumentElement; //XmlNodeList personNodes = rootElem.GetElementsByTagName("person"); //StorageFolder folder1 = null; // StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/city.plist")); // ////StorageFolder localFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; //foreach (StorageFolder folder in await localFolder.GetFoldersAsync()) //{ // if (folder.DisplayName == "Citys") // { // folder1 = folder; // break; // } //} //foreach (StorageFile file in await folder1.GetFilesAsync()) //{ // if (file.DisplayName == "city.plist") // { // file1 = file; // break; // } //} StorageFile file1 = null; string text; Citys city; List <Citys> citys = new List <Citys>(); var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("NewFolder"); foreach (StorageFile file in await folder.GetFilesAsync()) { if (file.Name == "XMLFile.xml") { file1 = file; break; } } if (file1 == null) { return; } IRandomAccessStream accesssStream = await file1.OpenReadAsync(); //string fileContent =await FileIO.ReadTextAsync(file1); using (StreamReader stream = new StreamReader(accesssStream.AsStreamForRead((int)accesssStream.Size))) { text = stream.ReadToEnd(); // DataContractSerializer serializer = new DataContractSerializer(typeof(Citys)); //city = (Citys)serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(text)) ); // city = (Citys)serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(text))); } string a = "<string>"; string b = "</string>"; int indexa = 1; int indexb = 1; for (int i = 0; i < text.Length; i++) { indexa = text.IndexOf(a, indexa + 1); indexb = text.IndexOf(b, indexb + 1); if (indexa == -1 && indexb == -1) { break; } string s = text.Substring(indexa + a.Length, indexb - indexa - a.Length); city = new Citys(); city.name = s; citys.Add(city); } citysName.ItemsSource = citys; //using (IInputStream inStream = await file1.OpenSequentialReadAsync()) //{ // DataContractSerializer serializer = new DataContractSerializer(typeof(Citys)); // city = (Citys)serializer.ReadObject(inStream.AsStreamForRead()); //} }