/// <summary> /// Считывание через SAX /// </summary> /// <param name="sFileName"></param> /// <param name="pPhone"></param> public static void LoadSAX(string sFileName, CPhone pPhone) { // SAX - simple API XML pPhone.ContactList.Clear(); using (Stream pStream = File.OpenRead( sFileName)) { using (XmlReader pReader = XmlReader.Create(pStream)) { pReader.Read(); if (!pReader.ReadToFollowing("phone")) { return; } if (!pReader.ReadToFollowing("contact")) { return; } //while (pReader.NodeType != XmlNodeType.Element) //{ // pReader.Read(); //} // pReader.ReadStartElement(); while ((pReader.NodeType == XmlNodeType.Element) && pReader.Name == "contact") { CContact pC = h_ReadContact(pReader); pPhone.ContactList.Add(pC); } // pRea } } }
public Form1() { InitializeComponent(); m_pPhone = new CPhone(); h_FillPhone(); h_RefreshListview(); }
/// <summary> /// Список контактов /// </summary> public static void SaveDOM(string sFileName, CPhone pPhone) { // DOM - document object model XmlDocument pDocument = new XmlDocument(); pDocument.InnerXml = "<storage/>"; XmlElement pE = pDocument.CreateElement("phone"); pDocument.DocumentElement.AppendChild(pE); foreach (CContact pC in pPhone.ContactList) { XmlElement pCNode = pDocument.CreateElement("contact"); pE.AppendChild(pCNode); var pAttr = pDocument.CreateAttribute("date"); pAttr.Value = pC.BirthDate.ToShortDateString(); pCNode.Attributes.Append(pAttr); XmlElement pP1Node = pDocument.CreateElement("phone"); pP1Node.InnerText = pC.Phone; pCNode.AppendChild(pP1Node); XmlElement pP2Node = pDocument.CreateElement("title"); pP2Node.InnerText = pC.Title; pCNode.AppendChild(pP2Node); } pDocument.Save(sFileName); }
/// <summary> /// Сохранение через SAX /// </summary> /// <param name="sFileName"></param> /// <param name="pPhone"></param> public static void SaveSAX(string sFileName, CPhone pPhone) { using (Stream pStream = File.Create( sFileName)) { XmlWriterSettings pSettings = new XmlWriterSettings() { Indent = true, Encoding = Encoding.GetEncoding(1251), }; using (XmlWriter pWriter = XmlWriter.Create( pStream, pSettings)) { pWriter.WriteStartDocument(); pWriter.WriteStartElement("root"); pWriter.WriteStartElement("phone"); foreach (var pC in pPhone.ContactList) { pWriter.WriteStartElement("contact"); pWriter.WriteAttributeString("date", pC.BirthDate.ToShortDateString()); pWriter.WriteElementString("phone", pC.Phone); pWriter.WriteElementString("title", pC.Title); pWriter.WriteEndElement(); } pWriter.WriteEndElement(); pWriter.WriteEndElement(); pWriter.WriteEndDocument(); } } }
/// <summary> /// Сохранить с помощью сериализации /// </summary> /// <param name="sFileName"></param> /// <param name="pPhone"></param> public static void SaveXML(string sFileName, CPhone pPhone) { using (Stream pStream = File.Create( sFileName)) { XmlSerializer pX = new XmlSerializer(pPhone.GetType()); pX.Serialize(pStream, pPhone); } }
/// <summary> /// Считать из сериализации /// </summary> /// <param name="sFileName"></param> /// <param name="pPhone"></param> public static void LoadXML(string sFileName, out CPhone pPhone) { using (Stream pStream = File.OpenRead( sFileName)) { XmlSerializer pX = new XmlSerializer(typeof(CPhone)); pPhone = (CPhone)pX.Deserialize(pStream); } }
public static void LoadDOM(string sFileName, CPhone pPhone) { pPhone.ContactList.Clear(); // DOM - document object model XmlDocument pDocument = new XmlDocument(); pDocument.Load(sFileName); XmlNode pPhoneNode = pDocument.DocumentElement .ChildNodes[0]; // XPath XmlNodeList pT = pPhoneNode .SelectNodes("//phone"); //foreach (XmlNode pN in pT) { // MessageBox.Show(pN.FirstChild.Value); //} }
private void button2_Click(object sender, EventArgs e) { CPhone pPhone = new CPhone(); CPhoneTool.LoadDOM("../$data/phone.xml", pPhone); foreach (var p in pPhone.ContactList) { MessageBox.Show(p.Title); } CPhoneTool.LoadSAX("../$data/phoneSax.xml", pPhone); foreach (var p in pPhone.ContactList) { MessageBox.Show(p.Title); } CPhone pPhone2; CPhoneTool.LoadXML("../$data/phoneXml.xml", out pPhone2); }