简体   繁体   中英

Creating XML using C# and an external DTD

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();
    }

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, ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM