Пример #1
0
 /// <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
         }
     }
 }
Пример #2
0
 public Form1()
 {
     InitializeComponent();
     m_pPhone = new CPhone();
     h_FillPhone();
     h_RefreshListview();
 }
Пример #3
0
        /// <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);
        }
Пример #4
0
        /// <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();
                }
            }
        }
Пример #5
0
 /// <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);
     }
 }
Пример #6
0
 /// <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);
     }
 }
Пример #7
0
        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);
            //}
        }
Пример #8
0
        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);
        }