Using C# to send and receive xml data

J

JD

Hi --
I've scanned a ton of google results and am sort of confused by all the
different objects that can be used for this. Here's what I need to do
(fairly simple) --

Construct an xml object in memory and fill it with some arbitrary
data.
Send this xml object to a web server.
Retrieve xml data from the server.

Any and all hints appreciated.

TIA

JD
 
K

Kevin Aubuchon

A dataset is one option. But if you are just looking for a vehicle to send
data, another option is to create a XML shema and use the XSD utility to
generate a custom class. Then just assign values to your class members. Use
the XmlSerializer to serialize the object to a stream, and send it. Do the
inverse to restore (deserialize) the object. Using a class will use fewer
bytes than a dataset, so sending/receiving will be faster.

Good luck,
kevin aubuchon
 
B

Bill Woodruff

JD wrote: >> Construct an xml object in memory >> and fill it with some arbitrary data.

JD, hope this gets you started Check out the article by Marc Clifton on CodeProject "Simple Serializer" for some very cool techniques including how to get the
contents of the BaseStream object of the XmlextWriter into a string : (Marc's article has links to his other interesting work with XML and XAML)

http://www.codeproject.com/csharp/SimpleSerializer.asp

best, Bill Woodruff
dotScience
Chiang Mai, Thailand

using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

private void createXmlInMemory()
{
XmlTextWriter theXMLTextWriter;

MemoryStream theMStream = new MemoryStream();
theXMLTextWriter = new XmlTextWriter(theMStream, new UTF8Encoding());

theXMLTextWriter.Formatting=Formatting.Indented;
theXMLTextWriter.Indentation = 8;
theXMLTextWriter.Namespaces=false;

// startDocument ...
theXMLTextWriter.WriteStartDocument();

theXMLTextWriter.WriteComment("Some comment");

// start element 1
theXMLTextWriter.WriteStartElement("Some element 1");

// start element 1.1
theXMLTextWriter.WriteStartElement("Some element 1.1");

// write out a value for element 1.1
theXMLTextWriter.WriteString("Value for object 1.1");

// close element 1.1
theXMLTextWriter.WriteEndElement();

// close element 1
theXMLTextWriter.WriteEndElement();

// end Document ...
theXMLTextWriter.WriteEndDocument();

theXMLTextWriter.Flush();

// at this point the BaseStream object of XMLTextWriter
// has the complete XML you have written ...
// do something with the data in the BaseStream here ...

theXMLTextWriter.Close();
}
 

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

Top