XML Document through a web service

C

Curt Krueger

Hello,
Long day for me, so apologies in advance if I'm asking where the trees
are in the middle of the forest.

I have wrote a errorlog object that stores it's data in XML format. What
I'm wanting to do is stream the XML through the web service, then to a
request made by ASP.NET.

I have successfully written one that will transmit it in binary format (see
code below), but what I'd like to is send it in its native format, XML plain
text. The ASP.NET consumer could then either parse it, or simply offer up
saving to disk.

Regards,
Curt


======================
Any web pages/code examples to do the following, only in "text" or format?


public byte[] GetErrorLog()
{
if (!File.Exists(LogPathAndFileName))
return null;
FileStream file = null;
try
{
//ensures one thread has access at a time
Monitor.Enter(this);
file = new FileStream(LogPathAndFileName, FileMode.Open);
byte[] fileStream = new byte[file.Length];
file.Read(fileStream, 0, (int)file.Length);
file.Close();
return fileStream;
}
catch (Exception ex)
{
return null;
}
finally
{
if (null != file)
file.Close();
Monitor.Exit(this);
}
}
 
D

Dino Chiesa [Microsoft]

have your webservice return a System.Xml.XmlElement. Something like the
following.

[WebMethod]
public System.Xml.XmlElement GetElement()
{
System.Xml.XmlDocument doc= new System.Xml.XmlDocument();
doc.Load(TheFile);
return doc.DocumentElement ;
}

The webservice client will get an XmlElement, which can be loaded into an
XmlDocument, or saved, or transformed into an HTML table, or whatever.

-Dino
 
C

Curt Krueger

Thanks for the help Dino. What I didn't realize is that I can take this
byte stream and send it down to the browser in the same format. As long as
I describe the content type in the response object, it automatically decodes
back to the original. Here's the code that works for anyone that's
interested.


private void btnDownload_Click(object sender, System.EventArgs e)
{
byte[] fileStream = null;

ErrorReporting er = new ErrorReporting(); // The web service call

fileStream = er.GetErrorLog(out error);
if (null != error)
return;

int length = fileStream.GetUpperBound(0) + 1;
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;
filename=errorlog.xml");
Response.AddHeader("Content-Length", length.ToString());
Response.ContentType = "application/octet-stream";
Response.OutputStream.Write(fileStream, 0, length);
Response.End();
}

Works like a charm!

Regards,
Curt


Dino Chiesa said:
have your webservice return a System.Xml.XmlElement. Something like the
following.

[WebMethod]
public System.Xml.XmlElement GetElement()
{
System.Xml.XmlDocument doc= new System.Xml.XmlDocument();
doc.Load(TheFile);
return doc.DocumentElement ;
}

The webservice client will get an XmlElement, which can be loaded into an
XmlDocument, or saved, or transformed into an HTML table, or whatever.

-Dino
======================
Any web pages/code examples to do the following, only in "text" or format?


public byte[] GetErrorLog()
{
if (!File.Exists(LogPathAndFileName))
return null;
FileStream file = null;
try
{
//ensures one thread has access at a time
Monitor.Enter(this);
file = new FileStream(LogPathAndFileName, FileMode.Open);
byte[] fileStream = new byte[file.Length];
file.Read(fileStream, 0, (int)file.Length);
file.Close();
return fileStream;
}
catch (Exception ex)
{
return null;
}
finally
{
if (null != file)
file.Close();
Monitor.Exit(this);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top