-1

I have classes generated from xsd that i would like to use to create an xml to send over the wire. I just want to create the document in memory, convert it to string/byte[] and send it. I was under the impression that once the classes are populated, i could just do a tostring() and it would return the entire document out. That doesn't seem to be the case... What am i doing wrong here?

@event myEvent = new @event();
myEvent.name = "AddProgram";
myEvent.version = 8.0M;

DateTime myDateTime = new DateTime();
myDateTime = DateTime.Now;
myEvent.time = myDateTime;

detail myDetail = new detail();
myDetail.name = "Program1"

myEvent.detail = myDetail;


Controller controller = new Controller();
controller.actionSpecified = true;
controller.action = ControllerAction.Create;

myDetail.Controller = controller;

String xmlString = myEvent.ToString();   //this is where i would expect a string.

all i get out of this is: "event"

Jason
  • 2,147
  • 6
  • 32
  • 40
  • 1
    Search the web for (for example) XML serialization, or protobuf – CodeCaster Sep 22 '14 at 19:02
  • 1
    take a look at this [link](http://stackoverflow.com/a/2434558/3877877) – Martin E Sep 22 '14 at 19:03
  • 1
    Why you are expecting that ToString() will return XML? Because it was made from XSD? It's not black box U can see at your code and assume that ToString() is not overided. U can write it yourself, but as it seems U are not much familiar with .net - use XmlSerializer to produce XML, XSD-generated classes must have attributes for this I think. – comdiv Sep 22 '14 at 19:03
  • I don't get to code in .Net as much as i'd like so i don't often step into the world of serialization and stream readers/writers. It makes sense now that i got a half decent example. – Jason Sep 22 '14 at 19:11

1 Answers1

1

I am not sure where you got your information that ToString() would give you an xml representation of the class but that is not true. What you should do is refer to this article about XML serialization.

http://msdn.microsoft.com/en-us/library/58a18dwa(v=vs.110).aspx

If you have a class of Type event then you would need to do the following to serialize it to XML, Also as a small tidbit I would stay away from using Key words as class or variable definitions if at all possible, but if you're not in control of that then your hands are tied.

@event myEvent = new @event();
myEvent.name = "AddProgram";
myEvent.version = 8.0M;

string xmlIWant= "";
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(@event);
using (StringWriter writer = new StringWriter())
{
   x.Serialize(writer, myEvent);
   xmlIWant = writer.ToString();
}
Bearcat9425
  • 1,580
  • 1
  • 11
  • 12
  • It's not that i really expected it to work. I have java code that works in similar manner but it had an overarching document type that would actually do a tostring and convert the whole document. I was sort of using that as my example and mixing it with the .net stuff. I just had a fundamental disconnect with the serialization. I had run through a bunch of examples and just could'nt quite put it together. – Jason Sep 22 '14 at 19:24
  • I see all is well just was curious at where you were getting that from. Not to say that you couldn't create your own ToStringXML extension method and insert a serialization snippet in there to where it would return and xml string. – Bearcat9425 Sep 22 '14 at 19:29