Not logged in. · Lost password · Register
Forum: agsXMPP RSS
Avatar
martinbach #1
Member since Mar 2009 · 60 posts
Group memberships: Members, Premium
Show profile · Link to this post
Subject: How to use my own object type based on "Creating your own packet types" example
Hi there, hi Alex,
how can I use my own object type (myType in the example below) in a class to send it via agsxmpp ?

eg:
public class Weather : Element
{
   public Weather ()
   { ... }

   public Weather ( ... )
   { ... }

   public int Humidity
   {
      get { ... }
      set { ... }
   }

   public int Temperature
   {
      get { ... }
      set { ... }
   }

   public myType MyType
   {
      get { ??? }        // <-----------
      set { ??? }        // <-----------
   }

}

public class myType
{
       string myString;      <------------
       int myInt;              <------------
       ...                        <------------
}

Thank you all for help,

Martin
Avatar
Alex #2
Member since Feb 2003 · 4328 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
you could use:

  1. public myType Foo
  2. {
  3.     get
  4.     {
  5.         return SelectSingleElement<myType>();
  6.     }
  7.     set
  8.     {       
  9.         if (HasTag<myType>())
  10.             RemoveTag<myType>();
  11.  
  12.         if (value != null)
  13.             this.AddChild(value);
  14.     }
  15. }

If you Browse the soure code of the agsXMPP protocol classes you can find many examples.
When you post an example of the XML you want to use I can help you to create the classes.
Avatar
martinbach #3
Member since Mar 2009 · 60 posts
Group memberships: Members, Premium
Show profile · Link to this post
Hi Alex,
Thanks I'll try that asap.
Martin
Avatar
martinbach #4
Member since Mar 2009 · 60 posts
Group memberships: Members, Premium
Show profile · Link to this post
Hi Alex,

the complexity of my future XML-File to send via agsxmpp should be like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Personen>
    <Person id="123456789" geschlecht="m" alter="30">
         <Vorname>Hans</Vorname>
        <Nachname>Muster</Nachname>
        <Adresse>
            <Strasse>
                <Strassenname>Hofstrasse</Strassenname>
                <Hausnummer>26</Hausnummer>
            </Strasse>
            <PLZ>20000</PLZ>
            <Ort>Hamburg</Ort>
        </Adresse>
    </Person>
    <Person id="123456790" geschlecht="w" alter="25">
        <Vorname>Theodora</Vorname>
        <Nachname>Tester</Nachname>
        <Adresse adressen_id="1">
            <Strasse>
                 <Strassenname>Gartenweg</Strassenname>
                 <Hausnummer>5</Hausnummer>
             </Strasse>
             <PLZ>80000</PLZ>
             <Ort>München</Ort>
        </Adresse>
        <Adresse adressen_id="2">
             <Strasse>
                 <Strassenname>Holzweg</Strassenname>
                 <Hausnummer>7</Hausnummer>
             </Strasse>
             <PLZ>82345</PLZ>
             <Ort>Holzhausen</Ort>
        </Adresse>
    </Person>
</Personen>

I'd be glad to get help from you to create the needed classes.

Thanks a lot
Martin
Avatar
Alex #5
Member since Feb 2003 · 4328 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
ok, can you post or attach the classes to have? Then I take a look at them and add the missing functions.

Alex
Avatar
martinbach #6
Member since Mar 2009 · 60 posts
Group memberships: Members, Premium
Show profile · Link to this post
Hi Alex,

this is my code so far:

namespace XmppWebService.Contracts.Elements
{
    public class PersonenType : agsXMPP.Xml.Dom.Element
    {
        public PersonenType()
        {
            this.TagName = "Personen";
            this.Namespace = "XmppWebService:Personen";
        }
    }

    public class PersonType : agsXMPP.Xml.Dom.Element
    {
        string id;
        string geschlecht;
        string vorname;
        string nachname;
        int alter;
        List <AdresseType> Adresse;
       
        public PersonType()
        {
            this.TagName = "Person";
            this.Namespace = "XmppWebService:Person";
        }

        public PersonType(string ID, string Geschlecht, string Vorname, string Nachname, int Alter, AdresseType Adresse)
        {
            this.id = ID;
            this.geschlecht = Geschlecht;
            this.vorname = Vorname;
            this.nachname = Nachname;
            this.alter = Alter;
        }

        public void AddAdresse(AdresseType Adresse)
        {
            this.Adresse.Add(Adresse);
        }

        public string ID
        {
            get { return this.GetAttribute("ID"); }
            set { this.SetAttribute("ID", value); }
        }

        public string Geschlecht
        {
            get { return this.GetAttribute("Geschlecht"); }
            set { this.SetAttribute("Geschlecht", value); }
        }

        public string Vorname
        {
            get { return this.GetAttribute("Vorname"); }
            set { this.SetAttribute("Vorname", value); }
        }

        public string Nachname
        {
            get { return this.GetAttribute("Nachname"); }
            set { this.SetAttribute("Nachname", value); }
        }

        public int Alter
        {
            get { return this.GetTagInt("Alter"); }
            set { this.SetTag("Alter", value); }
        }

        public AdressType Adresse
        {
            get { ??? ("Adresse"); }
            set { ??? ("Adresse", value); }
        }
    }

    public class StrasseType : agsXMPP.Xml.Dom.Element
    {
        string StrasseTypenname;
        string hausnummer;

        public StrasseType()
        {
            this.TagName = "Strasse";
            this.Namespace = "XmppWebService:Strasse";
        }

        public StrasseType(string Strassenname, string Hausnummer)
        {
            this.Strassenname = Strassenname;
            this.hausnummer = Hausnummer;
        }

        public string Strassenname
        {
            get { return this.GetAttribute("Strassenname"); }
            set { this.SetAttribute("Strassenname", value); }
        }

        public string Hausnummer
        {
            get { return this.GetAttribute("Hausnummer"); }
            set { this.SetAttribute("Hausnummer", value); }
        }
    }

    public class AdresseType : agsXMPP.Xml.Dom.Element
    {
        int plz;
        string ort;
        StrasseType StrasseType;
       
        public AdresseType()
        {
            this.TagName = "Adresse";
            this.Namespace = "XmppWebService:Adresse";
        }

        public AdresseType(int Plz, string Ort, StrasseType Strasse)
        {
            this.plz = Plz;
            this.ort = Ort;
            this.StrasseType = Strasse;
        }

        public int Plz
        {
            get { return this.GetAttributeInt("Plz"); }
            set { this.SetAttribute("Plz", value); }
        }

        public string Ort
        {
            get { return this.GetAttribute("Ort"); }
            set { this.SetAttribute("Ort", value); }
        }

        public StrasseType Strasse
        {
            get { return ??? ("Strasse"); }
            set { ??? ("Strasse", value); }
        }
    }

}


Thank you for your help

Martin
Avatar
Alex #7
Member since Feb 2003 · 4328 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
if I understand your Xml correct you can have multiple AdresseTypes.

To get all AdresseType use:

  1. public AdresseType[] GetAdresseTypes()
  2. {
  3.     ElementList el = SelectElements(typeof(AdresseType));
  4.     int i = 0;
  5.     AdresseType[] result = new AdresseType[el.Count];
  6.     foreach (AdresseTypes add in el)
  7.     {
  8.         result[i] = add;
  9.         i++;
  10.     }
  11.     return result;     
  12. }

You can get StrasseType in AdresseType with the following code:

  1. public StrasseType Strasse
  2. {
  3.     get { return SelectSingleElement(typeof(StrasseType)) as StrasseType; }
  4.     set
  5.     {
  6.         Element p = SelectSingleElement(typeof(StrasseType));
  7.         if (p != null)
  8.             p.Remove();
  9.  
  10.         AddChild(value);             
  11.     }
  12. }

let me know if this works for you.

Alex
Avatar
martinbach #8
Member since Mar 2009 · 60 posts
Group memberships: Members, Premium
Show profile · Link to this post
Hi Alex,

thank's  for the lines of code. Hopefully it will help.

Should it work for MatriX also ?

Martin
Avatar
Alex #9
Member since Feb 2003 · 4328 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
no, MatriX has a different Xml Api. MatriX is based on Linq to Xml.
See: http://msdn.microsoft.com/de-de/library/bb387098.aspx

Alex
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Forum: agsXMPP RSS