Not logged in. · Lost password · Register
Forum: MatriX and XmppDotNet RSS
Avatar
hardmanii #1
Member since Apr 2011 · 20 posts · Location: seoul
Group memberships: Members
Show profile · Link to this post
Subject: we would like to write xml messages like below code
we would like to write xml messages like below code

SEND:<message type="groupchat" to="dongpyo@domain" xmlns="jabber:client">
<body>say talk ...</body>
<UserList>
<User id="id1" name="myname1">
<User id="id2" name="myname2">
<User id="id3" name="myname3">
</UserList>
</message>



var msg = new Message { Type = MessageType.chat, To = _Jid, Body = msgBody };
msg.SetTag("UserList");
msg.SetElementValue("ChatType", "chatRoom");
msg.SetElementValue("id1", "myname1");
msg.SetElementValue("id2", "myname2");
msg.SetElementValue("id3", "myname3");
msg.EndTag();
This post was edited on 2011-04-19, 08:19 by hardmanii.
Avatar
Alex #2
Member since Feb 2003 · 4447 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
MatriX is build on System.Xml.Linq. So you can build packets with the System.Xml.Linq API or the extensions in MatriX.
For adding custom content to you packets I suggest to use the technology described here in the tutorial:
http://www.ag-software.net/matrix-xmpp-sdk/matrix-develope…

Alex
Avatar
jeffKotula #3
Member since Aug 2011 · 12 posts
Group memberships: Members
Show profile · Link to this post
Subject: Example for custom message content (similar to Iq example)
The tutorial example for the custom Iq is terrific. A specific example on adding custom message content would be helpful too. I get the general idea of how to extend it, but pointing me in the right direction regarding which specific functions/classes I need for simple messages would be great!
Avatar
Alex #4
Member since Feb 2003 · 4447 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
adding custom content to messages is nearly the same as adding id to iqs. Iq or message or presence is only the container to transport the data.

here is an exmaple how to add your custom XElement to a message
  1. var msg = new Message();
  2. msg.add(myType);

and how to retrieve it when your XElement is properly registered in the Factory.
  1. var myElement = msg.Element<myType>();

Alex
Avatar
jeffKotula #5
Member since Aug 2011 · 12 posts
Group memberships: Members
Show profile · Link to this post
Got it. Nice.

Suppose I wanted to send the contents of an existing class which can already serialize itself to an XML string?
Can I just directly add child elements to the Message?

var msg = new Message();
msg.add(myObject.toXML());

Or do I have to convert my XML string to a DOM first?
Avatar
Alex #6
Member since Feb 2003 · 4447 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Quote by jeffKotula:
var msg = new Message();
msg.add(myObject.toXML());

Or do I have to convert my XML string to a DOM first?

this will work and add your xml directly as value to the message element. I think this violates against the XMPP RFCs because you should use additional tags under the message tag for your payloads.

You have various options. You could add the Xml your serializer produces directly to the Body or any other tag of the message stanza. Then the html angle brackets of course get encoded. This looks not very nice but works great:

Example 1:
  1. string xml = "<foo>bar</foo>";
  2. Matrix.Xmpp.Client.Message msg = new Matrix.Xmpp.Client.Message();
  3. msg.Body = xml;

this build the followig Xml:
  1. <message xmlns="jabber:client">
  2.  <body>&lt;foo&gt;bar&lt;/foo&gt;</body>
  3. </message>

when you receive this packet you can deseriaize msg.Body with your Xml Deserializer.

Another option is to build a XElement from your serialized Xml output, either with the MatriX XmlLoader or the .NET Frameworks Xml Loaders which build the DOM for you.

Example 2:
  1. // MatriX Xml Loader
  2. string xml = "<foo xmlns='myns'>bar</foo>";
  3. Matrix.Xmpp.Client.Message msg = new Matrix.Xmpp.Client.Message();
  4. msg.Add(XmppXElement.LoadXml(xml));

Example 3:
  1. // .NET Framework Xml Loader
  2. string xml = "<foo xmlns='myns'>bar</foo>";
  3. Matrix.Xmpp.Client.Message msg = new Matrix.Xmpp.Client.Message();
  4. TextReader sr = new StringReader(xml);
  5. XElement xel = XElement.Load(sr);
  6. sr.Close();
  7. msg.Add(xel);

both produces the following xml:

  1. <message xmlns="jabber:client">
  2.  <foo xmlns="myns">bar</foo>
  3. </message>

The XmlLoader in MatriX is optimized for speed and XMPP. In some cases it produces Xml is is equal, but sometimes not exactly the same when you compare it char by char. This is a problem for some serializers, in the case I suggest to your the .NET Frameworks Xml loader.

Another option is to decode your xml or any other binary data base64 and just put the base64 in a tag. This works always.

Example 4:
  1. string xml = "<foo xmlns='myns'>bar</foo>";
  2. Matrix.Xmpp.Client.Message msg = new Matrix.Xmpp.Client.Message();
  3. msg.Body = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(xml));

gives you the following xml

  1. <message xmlns="jabber:client">
  2.  <body>PGZvbyB4bWxucz0nbXlucyc+YmFyPC9mb28+</body>
  3. </message>

let me know if this helps.

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: