コード例 #1
0
ファイル: CsvManager.cs プロジェクト: Baltasarq/Tacto
        public override PersonsList Import()
        {
            string[]    parts = null;
            PersonsList toret = new PersonsList();
            var         file  = new StreamReader(FileName);

            string line = file.ReadLine();

            while (!file.EndOfStream)
            {
                parts = SplitCsvLine(line);

                if (parts.Length >= 4)
                {
                    toret.Insert(new Person(parts[0], parts[1], parts[2], parts[3]));
                }
                else
                {
                    throw new ApplicationException("Bad CSV format");
                }

                line = file.ReadLine();
            }

            file.Close();

            return(toret);
        }
コード例 #2
0
        public override PersonsList Import()
        {
            string section;

            string[]    attributes;
            string      data;
            string      line;
            PersonsList toret = new PersonsList();
            Person      p     = null;

            var file = new StreamReader(FileName);

            Status = State.TopLevel;

            line = file.ReadLine().Trim();
            while (!file.EndOfStream)
            {
                if (line.Length > 0)
                {
                    SplitVcfLine(line, out section, out attributes, out data);

                    if (Status == State.TopLevel &&
                        section == EtqBeginSection &&
                        data.ToUpper() == EtqCardAttribute &&
                        attributes.Length == 0)
                    {
                        // Start vCard
                        Status = State.InCard;
                        p      = new Person();
                    }
                    else
                    if (Status == State.InCard &&
                        section == EtqEndSection &&
                        data.ToUpper() == EtqCardAttribute &&
                        attributes.Length == 0)
                    {
                        // Finish VCard
                        Status = State.TopLevel;
                        toret.Insert(p);
                        p = null;
                    }
                    else
                    if (Status == State.InCard)
                    {
                        // Fill fields
                        FillCard(p, section, attributes, data);
                    }
                    else
                    {
                        throw new ApplicationException("Misplaced statement:'"
                                                       + section + "'\nin '"
                                                       + line + "'\nwhile in state: "
                                                       + Status
                                                       );
                    }
                }

                line = file.ReadLine().Trim();
            }

            file.Close();
            return(toret);
        }
コード例 #3
0
ファイル: PersonsList.cs プロジェクト: Baltasarq/Tacto
        /// <summary>
        /// Loads the persons under a given XmlNode.
        /// </summary>
        /// <param name="mainNode">The main XML node, as a XmlNode.</param>
        /// <param name="toret">The PersonsList with the extracted info.</param>
        public static void LoadPersons(XmlNode mainNode, PersonsList toret)
        {
            // Run over all persons
            if (mainNode.Name.ToLower() == EtqPersons)
            {
                foreach (XmlNode node in mainNode.ChildNodes)
                {
                    string   surname    = "";
                    string   name       = "";
                    string   address    = "";
                    string   email      = "";
                    string   email2     = "";
                    string   web        = "";
                    string   wphone     = "";
                    string   hphone     = "";
                    string   mphone     = "";
                    string   id         = "";
                    string   comments   = "";
                    string[] categories = null;

                    if (node.Name.ToLower() == EtqPerson)
                    {
                        // Load name and surname
                        XmlNode nameNode = node.Attributes.GetNamedItem(EtqName);

                        if (nameNode != null)
                        {
                            name = nameNode.InnerText;
                        }
                        else
                        {
                            throw new XmlException(EtqName + " attribute should appear in node");
                        }

                        nameNode = node.Attributes.GetNamedItem(EtqSurname);

                        if (nameNode != null)
                        {
                            surname = nameNode.InnerText;
                        }
                        else
                        {
                            throw new XmlException(EtqSurname + " attribute should appear in node");
                        }

                        // Load the remaining data
                        foreach (XmlNode childNode in node.ChildNodes)
                        {
                            if (childNode.Name.ToLower() == EtqAddress)
                            {
                                address = childNode.InnerText;
                            }
                            else
                            if (childNode.Name.ToLower() == EtqEmail)
                            {
                                email = childNode.InnerText;
                            }
                            else
                            if (childNode.Name.ToLower() == EtqEmail2)
                            {
                                email2 = childNode.InnerText;
                            }
                            else
                            if (childNode.Name.ToLower() == EtqWeb)
                            {
                                web = childNode.InnerText;
                            }
                            else
                            if (childNode.Name.ToLower() == EtqWPhone)
                            {
                                wphone = childNode.InnerText;
                            }
                            else
                            if (childNode.Name.ToLower() == EtqHPhone)
                            {
                                hphone = childNode.InnerText;
                            }
                            else
                            if (childNode.Name.ToLower() == EtqMPhone)
                            {
                                mphone = childNode.InnerText;
                            }
                            else
                            if (childNode.Name.ToLower() == EtqId)
                            {
                                id = childNode.InnerText;
                            }
                            else
                            if (childNode.Name.ToLower() == EtqComments)
                            {
                                comments = childNode.InnerText;
                            }
                            else
                            if (childNode.Name.ToLower() == EtqCategory)
                            {
                                categories = childNode.InnerText.Split(';');
                            }
                        }

                        // Prepare categories
                        var cats = new List <Category>();
                        for (int i = 0; i < categories.Length; ++i)
                        {
                            var category = new Category(categories[i]);
                            if (!CategoryList.IsCategoryAll(category))
                            {
                                cats.Add(category);
                            }
                        }

                        // Add person
                        var p = new Person();
                        p.Surname     = surname;
                        p.Name        = name;
                        p.Email       = email;
                        p.WorkPhone   = wphone;
                        p.HomePhone   = hphone;
                        p.MobilePhone = mphone;
                        p.Address     = address;
                        p.Email2      = email2;
                        p.Web         = web;
                        p.Id          = id;
                        p.Comments    = comments;
                        p.Categories  = cats.ToArray();
                        toret.Insert(p);
                    }
                }
            }
            else
            {
                throw new XmlException("Invalid main label, expected '" + EtqPersons + "'");
            }

            return;
        }