7

I'm writing a client for a protocol that uses HTTP to transport XML messages. It is synchronous because I form an XML document that follows a DTD and send it to a gateway for the protocol via POST with the WebClient class and I get an XML response message from the remote server to indicate transaction state/message ID/etc.

Since I have the DTD, is it possible to create classes with it? There are a handful of possible responses for each type of "operation" my XML message is performing and having classes that could be hydrated by the returned server XML would be advantageous.

Once I have those classes, what are the basic steps to deserializing the XML message from the server into objects?

Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101

2 Answers2

4

Covert the DTD to XSD (not sure if this step is still required) :

Free DTD to XSD conversion utility?

Generate C# class from the XSD (command line tool, this is how I do it, not sure if there is a better way) :

http://quickstart.developerfusion.co.uk/quickstart/howto/doc/xmlserialization/XSDToCls.aspx

Serialize back to class from XML :

http://support.microsoft.com/kb/815813

Community
  • 1
  • 1
Eric H
  • 1,759
  • 1
  • 11
  • 14
  • This definitely gets me started on what I set out to do. I converted the DTD to XSD using trang and generated classes from the new XSD file. It should be easy to complete the code to serialize and deserialize from here. Thanks! – Jeff LaFay Jul 06 '11 at 19:03
1

once you have the xml string, you can do something like this where T is your generic object.

public static T GetObjectFromXmlString<T>(string xml)
{
    T result = default(T);

    if (string.IsNullOrEmpty(xml))
        return result;

    using (StringReader sr = new StringReader(xml))
    {
        using (XmlTextReader xr = new XmlTextReader(sr))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            result = (T)serializer.Deserialize(xr);
        }                   
    }

    return result;
}
Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
coder net
  • 3,447
  • 5
  • 31
  • 40
  • Nice, I'll have to try that. Would you happen to have a solution to the other part to my question? I would like a trivial process to generate the classes from the DTD. I'll try Eric H's suggestions but I was hoping .Net or VS2010 would have a tool to assist with that. – Jeff LaFay Jul 06 '11 at 18:19
  • i don't have a better way of generating classes from DTD. I'm not sure of your requirements but if you have to generate it once (dtd is static), i would go with some tool. but if you have to dynamically generate classes, you could read the dtd file and use reflection to create classes or assemblies. just a thought. – coder net Jul 06 '11 at 18:24
  • Yeah I went with a tool because it's static since it's a protocol. – Jeff LaFay Jul 06 '11 at 18:37
  • Btw, doesn't the using block close/dispose the StringReader and XmlTextReader? I don't think the explicit close() calls are necessary. – Jeff LaFay Jul 06 '11 at 19:10
  • You are right. I don't think you need it. I'll edit the answer. Also, this is meant to be a sample, please make sure to handle exceptions etc. – coder net Jul 06 '11 at 19:47