private void TestXmlRead() // public async Task ReadFromFile() { List <CityData> listLevels = new List <CityData>(); try { XmlReaderSettings settings = new XmlReaderSettings(); settings.Async = true; FileStream stream = File.OpenRead("cityAndCode.xml"); using (XmlReader reader = XmlReader.Create(stream, settings)) { // while (await reader.ReadAsync()) while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "row") { CityData stCity = new CityData(); while (reader.Read()) { if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "row") { listLevels.Add(stCity); break; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "city") { reader.Read(); stCity.m_strCityCname = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "code") { reader.Read(); stCity.m_iCityCode = Convert.ToInt32(reader.Value); } if (reader.NodeType == XmlNodeType.Element && reader.Name == "cityEn") { reader.Read(); stCity.m_strCityPinying = reader.Value; } } } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(string.Format(@"{0}:{1}", this.ToString(), ex.Message)); } }
// public async Task ReadFromFile() private void TestXmlRead() { List<CityData> listLevels = new List<CityData>(); try { XmlReaderSettings settings = new XmlReaderSettings(); settings.Async = true; FileStream stream = File.OpenRead("cityAndCode.xml"); using (XmlReader reader = XmlReader.Create(stream, settings)) { // while (await reader.ReadAsync()) while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "row") { CityData stCity = new CityData(); while (reader.Read()) { if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "row") { listLevels.Add(stCity); break; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "city") { reader.Read(); stCity.m_strCityCname = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "code") { reader.Read(); stCity.m_iCityCode = Convert.ToInt32(reader.Value); } if (reader.NodeType == XmlNodeType.Element && reader.Name == "cityEn") { reader.Read(); stCity.m_strCityPinying = reader.Value; } } } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(string.Format(@"{0}:{1}", this.ToString(), ex.Message)); } }