We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Reading XML file from a web service

jr_bautista
jr_bautista asked
on
Medium Priority
1,518 Views
Last Modified: 2012-05-06
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#)
Comment
Watch Question

Author

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.
anarki_jimbelSenior Developer
CERTIFIED EXPERT

Commented:

Author

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.
Top Expert 2009

Commented:
anarki_jimbelSenior Developer
CERTIFIED EXPERT

Commented:

Author

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?

Author

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>");
            sw.Flush();
            sw.Close();

        }    



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

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

 
Technical SEO Consultant
Commented:
You can use the xml classes to parse and format your data
// get the source xml into an XmlDocument
XmlDocument doc = new XmlDocument();
doc.LoadXml("<fieldname><f1></f1><f2></f2></fieldname>");
 
// 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
doc.Save(xmlWriter);
 
// display the result
Debug.WriteLine(stringWriter.ToString());

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Great!! this is what need....
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.