Not logged in. · Lost password · Register
Forum: MatriX RSS
Avatar
martinbach #1
Member since Mar 2009 · 60 posts
Group memberships: Members, Premium
Show profile · Link to this post
Subject: How to deserialize the iq object sent over XMPP back to my own type ?
Hi Alex,

now I can send my own type via XMPP in a iq object.

My next challenge is to deserialize the e.iq to my own object type.

1. Question - Why is    xdoc == null   true  using the following:   XDocument xdoc = e.Iq.Document

Details:
void xmppClient_OnIq(object sender, IqEventArgs e)
{
     ...
     if (e.Iq.Type == Matrix.Xmpp.IqType.get)
     {
           ...
           WriteToChatReceive(e.Iq.ToString());  // prints out the whole xml tree as I would expect
           XDocument xdoc = e.Iq.Document;     // xdoc gets the XDocument from e.Iq or not ?
           MessageBox.Show((xdoc == null).ToString());  // should result in "False" but result is "True" !!??
           ...
     }
     ...
}

2. Question

Is there a more automatic / generic / elegant way to deserialize the xml content of e.iq  to my own type ?
I currently use the following:

                var query = from xElem in xdoc.Descendants("Person")
                            select new Person
                            {
                                Vorname = xElem.Element("Vorname").Value,
                                Nachname = xElem.Element("Nachname").Value,
                                Alter = Int16.Parse(xElem.Element("Alter").Value),
                                ....
                            };

It doesn't work till now cause my problem with xdoc = e.Iq.Document of question 1 I asked above,
but I hope it will work after solving question 1.

Thanks for your help.

Martin
Avatar
Alex #2
Member since Feb 2003 · 4328 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
you have to register your custom packet in the Factory, then MatriX will deserialize your packets automatically.
Can you post a small code snippet how you build your Iq, and the Xml (iq.ToString()) of your packet. Then I will post the code how to access you own objects after deserialization.
Avatar
martinbach #3
Member since Mar 2009 · 60 posts
Group memberships: Members, Premium
Show profile · Link to this post
Hi Alex here are the snippets and the output if Iq:
(With these snippets and other code I can create Iq, send it via XMPP and get the right output but no deserialization)

Snippet to register:

private void RegisterXmppElements()
{
    XMPPWebService.Contracts.Elements.Adresse ai = new XMPPWebService.Contracts.Elements.Adresse();
    XMPPWebService.Contracts.Elements.PersonInfo pi = new XMPPWebService.Contracts.Elements.PersonInfo();           
    Matrix.Xml.Factory.RegisterElement<XMPPWebService.Contracts.Elements.Adresse>("mynamespace:mns", "Adresse");
    Matrix.Xml.Factory.RegisterElement<XMPPWebService.Contracts.Elements.PersonInfo>("mynamespace:mns", "PersonInfo");
}


Snippet to build Iq:

Matrix.Xmpp.Client.Iq PersonInfoIq = new Matrix.Xmpp.Client.Iq();
PersonInfoIq.GenerateId();
PersonInfoIq.To = serviceJid;
PersonInfoIq.Type = Matrix.Xmpp.IqType.get;
WriteToChatSend("PersonInfoIq.Type = " + PersonInfoIq.Type.ToString() + "  serviceJid = " + serviceJid.ToString());
XMPPWebService.Contracts.Elements.Adresse ai = new Adresse("Wallstreet", 45);               
XMPPWebService.Contracts.Elements.PersonInfo pi =
             new XMPPWebService.Contracts.Elements.PersonInfo("uid4294194", "aid35937", 1234567, "Martin", "Bach", 12, ai);
PersonInfoIq.Add(pi);
this.xmppClient.Send(PersonInfoIq);



Output of e.Iq.ToString():

<iq id="MX_4" to="martin1@abcd/MatriX" type="get" from="martin2@abcd/MatriX" xmlns="jabber:client">
  <PersonInfo xmlns="mynamespace:mns" uid="uid4294194" aid="aid35937" lfd="1234567">
    <Vorname>Martin</Vorname>
    <Nachname>Bach</Nachname>
    <Alter>12</Alter>
    <Adresse>
      <Strasse>Wallstreet</Strasse>
      <Hausnr>45</Hausnr>
    </Adresse>
  </PersonInfo>
</iq>

Now I'm curious to read how deserialization and access of my own object works.

Martin
This post was edited on 2009-06-29, 20:51 by martinbach.
Avatar
Alex #4
Member since Feb 2003 · 4328 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
when you have registered you packets in the factory then following code retrieves your object.

  1. var personInfo = iq.Element<PersonInfo>();

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

I had a typo in a namespace string and therefore ?! didn't get any result on iq.Element<PersonInfo>().

Now it works, thank you.

Martin
Avatar
Alex #6
Member since Feb 2003 · 4328 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Quote by martinbach:
I had a typo in a namespace string and therefore ?! didn't get any result on iq.Element<PersonInfo>().

ya, the namespaces and tag names must match exactly in your classes and the factory.
I often use constants for both to eliminate typos.

Alex
Avatar
martinbach #7
Member since Mar 2009 · 60 posts
Group memberships: Members, Premium
Show profile · Link to this post
ya, good idea   :-D  

Martin
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: MatriX RSS