Пример #1
0
        private static vCard ReadRootElement(XmlDocument doc, ExtendedPropertiesReader xreader = null)
        {
            vCard c = new vCard();
              XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
              nsm.AddNamespace("vc", vCard.vcard40NS);

              XmlNode xcards = doc.SelectSingleNode("/vc:vcards", nsm);
              if (!(xcards is XmlElement))
            throw new FormatException("Missing <vcards> element");

              XmlNode xcard = xcards.SelectSingleNode("vc:vcard", nsm);
              if (!(xcard is XmlElement))
            throw new FormatException("Missing /vcards/vcard element");

              string fn = xcard.SelectOptionalInnerText("vc:fn/vc:text", nsm);
              c.Fn.Items.Add(new vCardMultiString(fn));
              c.EMail.Items.Add(new vCardMultiString(xcard.SelectOptionalInnerText("vc:email/vc:text", nsm)));
              c.Name = xcard.SelectOptionalInnerText("vc:name/vc:text", nsm);
              c.Source.Items.Add(new vCardMultiString(xcard.SelectOptionalInnerText("vc:source/vc:uri", nsm)));

              XmlNode xn = xcard.SelectSingleNode("vc:n", nsm);
              if (xn is XmlElement)
              {
            c.N = new vCardN();
            c.N.FamilyName = xn.SelectOptionalInnerText("vc:surname/vc:text", nsm);
            c.N.GivenNames = xn.SelectOptionalInnerText("vc:given/vc:text", nsm);
              }

              if (xreader != null)
            xreader((XmlElement)xcard, nsm, c);

              return c;
        }
Пример #2
0
 void ParseExtensions(XmlElement xcard, XmlNamespaceManager nsm, vCard vcard)
 {
     nsm.AddNamespace("ex", "http://here.net");
       XmlNode xwhy = xcard.SelectSingleNode("ex:why/text()", nsm);
       if (xwhy != null)
     vcard.AddExtendedProperty("why", "http://here.net", new vCardTextProperty(xwhy.Value));
 }
Пример #3
0
 public void CanAddExtendedUriProperty()
 {
     vCard v = new vCard();
       v.AddExtendedProperty("where", "http://here.net", new vCardUriProperty("ldap://somefellow"));
       vCardUriProperty p = v.GetExtendedProperty("where", "http://here.net") as vCardUriProperty;
       Assert.IsNotNull(p);
       Assert.AreEqual("ldap://somefellow", p.ToString());
       Assert.AreEqual("ldap://somefellow", p.Uri);
 }
Пример #4
0
 public void CanAddExtendedTextProperty()
 {
     vCard v = new vCard();
       v.AddExtendedProperty("why", "http://because.net", new vCardTextProperty("It is red"));
       vCardTextProperty p = v.GetExtendedProperty("why", "http://because.net") as vCardTextProperty;
       Assert.IsNotNull(p);
       Assert.AreEqual("It is red", p.ToString());
       Assert.AreEqual("It is red", p.Text);
 }
Пример #5
0
        public void Write(Stream s, vCard c)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
              settings.Encoding = Encoding.UTF8;
              settings.Indent = true;
              settings.IndentChars = " ";

              using (XmlWriter w = XmlWriter.Create(s, settings))
              {
            Write(w, c);
              }
        }
Пример #6
0
 private void VerifyOutput(vCard c, string expectedOutput)
 {
     using (MemoryStream s = new MemoryStream())
       {
     vCardXmlWriter writer = new vCardXmlWriter();
     writer.Write(s, c);
     s.Seek(0, SeekOrigin.Begin);
     StreamReader reader = new StreamReader(s);
     string output = reader.ReadToEnd();
     Assert.AreEqual(expectedOutput, output);
       }
 }
Пример #7
0
        public void CanWriteEmptyvCard()
        {
            vCard v = new vCard();
              v.Fn.Items.Add(new vCardMultiString("John Doe"));

              VerifyOutput(v, @"<?xml version=""1.0"" encoding=""utf-8""?>
            <vcards xmlns=""urn:ietf:params:xml:ns:vcard-4.0"">
             <vcard>
              <fn>
               <text>John Doe</text>
              </fn>
             </vcard>
            </vcards>");
        }
Пример #8
0
        public void CanWritevCardWithExtendedProperties()
        {
            vCard v = new vCard();
              v.Fn.Items.Add(new vCardMultiString("John Doe"));

              v.AddExtendedProperty("where", "http://here.net", new vCardUriProperty("ldap://somefellow"));
              v.AddExtendedProperty("why", "http://because.net", new vCardTextProperty("It is red"));
              VerifyOutput(v, @"<?xml version=""1.0"" encoding=""utf-8""?>
            <vcards xmlns=""urn:ietf:params:xml:ns:vcard-4.0"">
             <vcard>
              <fn>
               <text>John Doe</text>
              </fn>
              <ex:where xmlns:ex=""http://here.net"">
               <ex:uri>ldap://somefellow</ex:uri>
              </ex:where>
              <ex:why xmlns:ex=""http://because.net"">
               <ex:text>It is red</ex:text>
              </ex:why>
             </vcard>
            </vcards>");
        }
Пример #9
0
        public void Write(XmlWriter w, vCard c)
        {
            if (c.Fn.Items.Count == 0)
            throw new ArgumentException("vCard must have non-empty FN property", "FN");

              w.WriteStartDocument();

              w.WriteStartElement("vcards", vCard.vcard40NS);
              w.WriteAttributeString("xmlns", "", null, vCard.vcard40NS);
              w.WriteStartElement("vcard");

              if (c.Source.Default != null)
            WriteUriElement(w, "source", c.Source.Default.Value.ToString());
              if (c.Name != null)
            WriteTextElement(w, "name", c.Name);
              WriteTextElement(w, "fn", c.Fn.Default.Value);
              //WriteTextElement(w, "email", c.EMail);

              if (c.N != null && (!string.IsNullOrEmpty(c.N.FamilyName) || !string.IsNullOrEmpty(c.N.GivenNames)))
              {
            w.WriteStartElement("n");
            if (c.N.FamilyName != null)
              WriteTextElement(w, "surname", c.N.FamilyName);
            if (c.N.GivenNames != null)
              WriteTextElement(w, "given", c.N.GivenNames);
            w.WriteEndElement();
              }

              foreach (vCard.PropertyRegistration reg in c.PropertyRegistrations)
              {
            reg.Property.WriteXml(w, reg.PropertyName, reg.PropertyNS);
              }

              w.WriteEndElement(); // vcard
              w.WriteEndElement(); // vcards

              w.WriteEndDocument();
        }
Пример #10
0
        public vCard Read(HtmlDocument doc)
        {
            vCard c = ReadRootElement(doc);

            return(c);
        }