ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » IBM MQ API Support » Send ascii file through C# code

Post new topic  Reply to topic
 Send ascii file through C# code « View previous topic :: View next topic » 
Author Message
johgri
PostPosted: Sat Nov 09, 2013 7:46 am    Post subject: Send ascii file through C# code Reply with quote

Newbie

Joined: 09 Nov 2013
Posts: 2

Hi,
I have searched in the internet and I have written a sample code in c# for sending an ascii file through MQ. But all I have found are about sending a string and not a file. I have written the following code, using streamfile and fill a string variable named "text". Then I use this variable to send it through MQ queue :

public void GetFile()
{
String path = "C:\\file.txt";
StreamReader streamReader = new StreamReader(path);
text = streamReader.ReadToEnd();
streamReader.Close();
}


public void PutMessageOnQueue(string message)
{
try
{
message = text;
queue = queueManager.AccessQueue(queueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);

queueMessage = new MQMessage();
queueMessage.WriteString(message);
queueMessage.Format = MQC.MQFMT_STRING;
queue.Put(queueMessage);

}

catch (MQException mqexp)
{

Console.WriteLine("MQSeries Exception: " + mqexp.Message);

Console.WriteLine("MQSeries Exception: " + mqexp.Reason);

Console.WriteLine("MQSeries Exception: " + mqexp.StackTrace);

}

}
Can you please help me with the code?

Regards,

John
Back to top
View user's profile Send private message
mqjeff
PostPosted: Sat Nov 09, 2013 9:36 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

A file is just a string of bytes.
Back to top
View user's profile Send private message
gbaddeley
PostPosted: Sun Nov 10, 2013 2:11 pm    Post subject: Reply with quote

Jedi

Joined: 25 Mar 2003
Posts: 2492
Location: Melbourne, Australia

Your code design is correct. Read the entire file into a string (or some other suitable buffer type, eg. byte array). Put the string as an MQ message.
_________________
Glenn
Back to top
View user's profile Send private message
johgri
PostPosted: Tue Nov 19, 2013 3:48 pm    Post subject: Reply with quote

Newbie

Joined: 09 Nov 2013
Posts: 2

Hello,
Message is written in the queue, but although the ascii file has 5 lines, MQ queue writes the data in one single line. I was expected to have a message with 5 lines separated by carriage return character.
I have changed C# code using an array:
mqMsg = new MQMessage();
string[] lines = System.IO.File.ReadAllLines(@"C:\\Users\\vdt003.txt");

foreach (string line in lines)
{

message = line ;
msgLen = message.Length;

if (msgLen > 0)

.................
}
Can you please help?

Thanks John
Back to top
View user's profile Send private message
bruce2359
PostPosted: Tue Nov 19, 2013 3:56 pm    Post subject: Reply with quote

Poobah

Joined: 05 Jan 2008
Posts: 9394
Location: US: west coast, almost. Otherwise, enroute.

If you imbed CR/LF characters in the data you put to the queue, they will be there when you get the message out of the queue. Messages are not files, and therefore to not adhere to file protocols like CR/LF.
_________________
I like deadlines. I like to wave as they pass by.
ב''ה
Lex Orandi, Lex Credendi, Lex Vivendi. As we Worship, So we Believe, So we Live.
Back to top
View user's profile Send private message
mqjeff
PostPosted: Wed Nov 20, 2013 3:13 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

An MQ message is just a string of bytes.

A string of bytes doesn't know that 0x0a0x0d is a "line ending".
Back to top
View user's profile Send private message
gbaddeley
PostPosted: Wed Nov 20, 2013 2:33 pm    Post subject: Reply with quote

Jedi

Joined: 25 Mar 2003
Posts: 2492
Location: Melbourne, Australia

johgri wrote:
string[] lines = System.IO.File.ReadAllLines(@"C:\\Users\\vdt003.txt");

You need to read the entire file into a single string as one entity, not as a sequence of records (appended together without line terminators).
_________________
Glenn
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » IBM MQ API Support » Send ascii file through C# code
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.