StreamReader

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am receiving xml via tcp/ip and want to save it into a msmq queue.
when I use the StreamReader.ReadLine() it only reads one line of the
stream. but my xml could be more than 1 line.
when I use StreamReader.ReadToEnd() it just listens to the port until the
connection is closed and then it writes it to the queue, which is not the
method that i want because more than one xml are saved to a queue at a time.
basicly i need a read method that receives 1 xml that comes through and
saves it to the queue.

thanks in advance
 
How are you constructing your StreamReader?
Are you using a WebResponse object?

In my code, I create a WebResponse object from a WebRequest object, I then
create my stream as follows

Stream oStream = oWebResponse.GetResponseStream();
Encoding oEncoding = System.Text.Encoding.GetEncoding("utf=8");
StreamReader oStreamReader = new StreamReader(oStream, oEncoding);
string sXmlData = oStreamReader.ReadToEnd();
 
I had to use something like this once:

string thisline = "";
while(StreamReader.Peek() != null)
{
thisline += StreamReader.ReadLine();
if(thisline.IndexOf("</XMLROOTELEMENT>")!=-1)
{
sendToQue(thisline);
thisline = "";
}
}
 
I am trying to use your code but i get an error on oWebresponse: use of
unassigned local variable:

WebResponse oWebResponse;

Stream ostream= oWebResponse.GetResponseStream();
StreamReader oStreamReader= new StreamReader(ostream);
string strXMLData=oStreamReader.ReadToEnd();

Thanks
 
I didn't include that code coz I didn't think the content of the WebResponse
was relevant. I don't think it's applicable to what you're doing, but here
you go

StringBuilder sbPayload = new StringBuilder();

sbPayload.Append("<?xml version=\"1.0\"?>");

sbPayload.Append("<a:searchrequest xmlns:a=\"DAV:\">");

sbPayload.Append("<sql> SELECT \"urn:schemas:contacts:fileas\" ");

sbPayload.Append(", \"urn:schemas:contacts:givenName\" ");

sbPayload.Append(", \"urn:schemas:contacts:sn\" ");

sbPayload.Append(", \"urn:schemas:contacts:o\" ");

sbPayload.Append("FROM Scope('SHALLOW TRAVERSAL OF \"\"') ");

sbPayload.Append("Where \"DAV:isfolder\" = false AND \"DAV:contentclass\"
");

sbPayload.Append("= 'urn:content-classes:person'");

sbPayload.Append(" ORDER BY \"urn:schemas:contacts:fileas\" ASC");

sbPayload.Append("</>");

sbPayload.Append("</>");

// Array to hold Xml Payload

byte[] arPayload = null;

try

{

// Get Payload and Encode it to utf-8

arPayload = Encoding.UTF8.GetBytes((string)sbPayload.ToString());

// Create HTTP Web Request & Set Properties

HttpWebRequest oWebRequest =
(System.Net.HttpWebRequest)HttpWebRequest.Create(sSourceURL);

oWebRequest.Method = "SEARCH";

oWebRequest.ContentType = "text/xml";

oWebRequest.ContentLength = arPayload.Length;

// Inject the Search Payload into the RequestStream

Stream oRequestStream = oWebRequest.GetRequestStream();

oRequestStream.Write(arPayload, 0, arPayload.Length);

oRequestStream.Close();


// Set Credentials to Access Exchange Store

oWebRequest.Credentials = CreateCredentialCache(sSourceURL);

// Create the Web Response Object

System.Net.WebResponse oWebResponse = (System.Net.WebResponse)
oWebRequest.GetResponse();

// Get the Xml Response Stream
 
Back
Top