Jump to content
  • Advertisement
Sign in to follow this  
Shashwat Rohilla

How to use .Net (C#) classes to generate XML strings?

This topic is 2477 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

C#

I want to send an XML string over network and then parse it using predefined XML classes.
I tried XmlTextWriter, but it asks to a Stream object to initialize.
I can have Console.Out there as

XmlTextWriter textwriter = new XmlTextWriter(Console.Out);
then I can use
textwriter.WriteStartDocument()
textwriter.WriteEndDocument()
textwriter.WriteStartElement()
textwriter.WriteEndElement()

methods.

But still I didn't find any way to convert it to string or byte[] to send it to socket.
Also I didn't find any way to parse the XML data using these classes.
Please help!

Share this post


Link to post
Share on other sites
Advertisement
You could try:


var xDoc = new XDocument();
var rootNode = new XElement("RootNode");
var childNode = new XElement("ChildNode");

rootNode.Add(childNode);
childNode.SetAttributeValue("Name", "foo");
childNode.Value = "Today is a nice day";
xDoc.Add( rootNode );

var stringToSend = xDoc.ToString());

// for simplicity, the '=' is our network
var stringReceived = stringToSend;

var someOtherXDoc = XDocument.Parse( stringReceived );


best Jochen

Share this post


Link to post
Share on other sites
The XmlSerializer and Deserializer are what you're looking for.


I saw them, they also require those Streams. I do not want to include any files in these.
The jochen's is exactly I was looking for.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!