tags:

views:

55

answers:

2

Hi there,

This is my first steps with XML and I must send a XML by HttpRequest (Which is not a problem to me now). By I've a question about DTDs. According to the HttpRequest destination APIs, I must validate my XML using an External DTD whos located there (this is for Canada Post shipping : http://cybervente.postescanada.ca/DevelopersResources/protocolV3/eParcel.dtd). I know how to write / read XML, but not according to DTD... Is there a difference?

Can someone tell me how and the easiest way to do that? I've look a good part of good post from Google and there's never what I'm looking for ...

Thank you!

ADD #1

Note : I know what a DTD for, and I can create one on my own with a plain text editor and basing the XML on the DTD, but I realy mean, is there a way to take advantage of DTD in C# (Creating an object or someting...)

ADD #2 Add-on : Any of you guys already set up an application to talk to Canada Post API using webresque? Because I'm stunk! I send my request with my data and it never finish so never return response ... here is my code :

public oShippingResponse RetreiveShippingCost(oShippingInformations shipInfos) {
        // Send request                             
        WebRequest request = WebRequest.Create("http://sellonline.canadapost.ca");
        XmlDocument xmlDoc = shipInfos.WriteAsXML();
        request.ContentType = "text/xml";
        request.Method = "POST";

        xmlDoc.Save(request.GetRequestStream());
        try {
            WebResponse response = request.GetResponse();
        } catch (Exception ex) {
            throw ex;
        }
        return new oShippingResponse();
    }
+1  A: 

No, there is no difference in how you write your XML, other than that you should obey the rules laid out in the DTD. Understanding and reading a DTD is an art, so I hope that Canada Post has a more descriptive way of explaining the format to you to aid you in creating the correct XML.

Then, what Canada Post requests, you should validate your XML against the DTD. While being valid doesn't mean that the input is correct, it should warn you early about invalid input. And that's precisely why they want you to do this: if your output is guaranteed correct against the DTD, they can guarantee you that they can process the input (in most cases, at least).

Here's how you can validate your input against a DTD using C# on Microsoft Support.

Note on editing the XML by hand: most XML editors are capable of reading a DTD and warning you that the DTD is correct, or even give you syntax help while you type, i.e. in Visual Studio. The XML standard demands that if a DTD is present in the header of the XML, the XML itself must be validated and must not be processed if not valid against the DTD.

Abel
Yep sure, they show an exemple of what our XML should like too somewhere else in a PDF document. I've found everyting exept exemple of code of how to use it. So, thats why I've asked you guys. But, thats ok, I'll do what you show me with this link! Thanks !
Simon
@Simon, you're welcome :). I edited the answer meanwhile. Using the DTD can also aid you while editing the XML by hand.
Abel
According your note : Pefect. But I'm doing it programmaticaly with objects. Object gonna have a "WriteXML()".
Simon
Ok, and how can I use it programmaticaly?
Simon
@Simon: If you use `System.Xml` methods for the `WriteXML()` method (you really should, don't even try to write the output by hand!), then you can preload the DTD when you create your XML object. This will give you an early warning when you try to write anything not allowed by the DTD.
Abel
Abel, the article on Microsoft Support you link to suggests to use XmlValidatingReader, which is obsolete in .net 2.0, see: http://msdn.microsoft.com/en-us/library/system.xml.xmlvalidatingreader.aspx, instead see: http://msdn.microsoft.com/en-us/library/hdf992b8.aspx
sgrassie
Sorry about that. I'll update the question with how to *write* code (it wasn't about reading in the first place, was it?)
Abel
Where is the "WriteXML()" methods you were talking about?
Simon
@simon, erhm, *you* were talking about it. Quote: *"Object gonna have a WriteXML()"*. Did I misunderstand something?
Abel
No its ok, - I - were misunderstanding something! Sorry!
Simon
+1  A: 

You need to create a validating XML reader. You'll need an XmlSchemaSet to store the schema in, and you'll need an XmlReaderSettings object to set configuration options up for the XmlReader. Something like (untested):

var schemaSet = new XmlSchemaSet();
schemaSet.Add(null, pathToSchema);

var settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.DTD;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.Schemas = schemas;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.ValidationEventHandler += ValidationHandler;

using(var fstream = new FileStream(pathToDocument))
{
    using(var reader = XmlReader.Create(documentStream, settings))
    {
        while(reader.Read())
        {
        }
    }
}

In the ValidationHandler you can do stuff like catch any validation errors/warnings that you might be interested in outputting.

sgrassie
I'll test it and come back waht feedback! Thank u sgrassie!
Simon