• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1487
  • Last Modified:

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#)
  • 5
  • 2
  • 2
  • +1
1 Solution
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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:

Tony McCreathTechnical 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:
Great!! this is what need....

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now