Reading XML file from a web service

How can I write the code to read the XML file from a web service? When it consume the web service it should return the records it gets from the XML file.

Please provide me the code snippets both for web service(in C#) and to consume the webservice(C#)
Who is Participating?
Tony McCreathConnect With a Mentor Technical SEO ConsultantCommented:
You can use the xml classes to parse and format your data
// get the source xml into an XmlDocument
XmlDocument doc = new XmlDocument();
// create an xml writer that will write indented xml into a string
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 4;
xmlWriter.IndentChar = ' ';
xmlWriter.QuoteChar = '"';
// send the xml to the writer
// display the result

Open in new window

jr_bautistaAuthor Commented:
I already have a running Web service. What I am looking for is the code to read this XML file(attached) and return via web service.
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

jr_bautistaAuthor Commented:
As I said, I already can consume web service but for a single data. E. x. I can return one variable which contains the result of a computation. What I'm looking for is the code to read the records from a XML file and return it to the client requesting. Since it contains lots of records I have no idea to return it to the client.
jr_bautistaAuthor Commented:
Is there a way I can load the XML file to a dataset(generated by web service) then I can consume the ws and return to me the dataset or I should do it line by line to write in a text/XML file?
jr_bautistaAuthor Commented:
I was able to read and return to the XML file to the WS

I used System.IO to write it to the text file. My question how can I make the format not in one line, if I open the XML file and manually added <root> tag in visual studio the format is ok(with all the indention). If I do it programmatically, it goes one line. Thanks

Here's my C# code snippet:
using (StreamWriter sw = File.CreateText(filename))
            sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?><root>" + ws.HelloWorld1() + "</root>");


Current format : <fieldname><f1></f1><f2></f2></fieldname>

Desired format:

jr_bautistaAuthor Commented:
Great!! this is what need....
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.