Menu

[r40]: / trunk / WhatsappClient / Classes / ContactCollection.cs  Maximize  Restore  History

Download this file

112 lines (91 with data), 3.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
/*
* by Stefan Bijen aka Freakypain - stefan.bijen@gmail.com
* by Swen Kooij aka Kirk - swenkooij@gmail.com
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;
public class ContactCollection : List<Contact>
{
/// <summary>
/// Represents a collection of contacts
/// </summary>
public ContactCollection(string Filename)
{
Constructor(Filename);
}
/// <summary>
/// Constructor overload, loads from contacts.xml in the current directory.
/// </summary>
public ContactCollection()
{
Constructor("contacts.xml");
}
/// <summary>
/// Serves as internal constructor, to make correct overloads of the constructor work
/// </summary>
/// <param name="Filename">The file path of the XML file.</param>
private void Constructor(string Filename)
{
// Check if the file exists, if not throw an exception
if (System.IO.File.Exists(Filename) == false)
{
ToFile();
}
// create an xml document object.
XmlDocument xmlDoc = new XmlDocument();
// Load the XML from the specified file.
xmlDoc.Load(Filename);
// Get all 'contact' elements
XmlNodeList XmlContacts = xmlDoc.GetElementsByTagName("contact");
// Loop through nodes & save them to list
foreach (XmlNode Node in XmlContacts)
{
// Create new instance of Contact class
Contact Con = new Contact(Node.ChildNodes[1].InnerText, Node.ChildNodes[0].InnerText, Node.Attributes["name"].InnerText);
this.Add(Con);
}
}
/// <summary>
/// Ooutputs all contacts to a file
/// </summary>
/// <param name="Filename">The name/location of the file the XML should be written to.</param>
public void ToFile(string Filename)
{
List<Contact> AllContacts = this;
XmlTextWriter writer = new XmlTextWriter(Filename, null);
// Set indenting so that its easier to read XML when open in Notepad.
writer.Formatting = Formatting.Indented;
// This will output the XML declaration
writer.WriteStartDocument();
// Now let's add root element.
writer.WriteStartElement("contacts");
foreach (Contact Cont in AllContacts)
{
// Let's add a node
writer.WriteStartElement("contact");
// We can also add an attribute to "person" node
writer.WriteAttributeString("name", Cont.Name);
// If you want to add children nodes to "person", here is where you do it.
writer.WriteElementString("country_code", Cont.CountryCode);
writer.WriteElementString("phonenumber", Cont.PhoneNumber);
// Close "person" node
writer.WriteEndElement();
}
// Close "contact" root element
writer.WriteEndElement();
writer.WriteEndDocument();
// This will write the above elements to the file itself
writer.Close();
}
/// <summary>
/// Overload, writes to contact.xml
/// </summary>
public void ToFile()
{
ToFile("contacts.xml");
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.