使用 C# 和外部 DTD 创建 XML

发布于 2024-09-12 14:47:36 字数 1323 浏览 2 评论 0原文

这是我使用 XML 的第一步,我必须通过 HttpRequest 发送 XML(现在这对我来说不是问题)。我有一个关于 DTD 的问题。根据 HttpRequest 目标 API,我必须使用位于此处的外部 DTD 验证我的 XML(这是用于加拿大邮政运输:http://cybervente.postescanada.ca/DevelopersResources/protocolV3/eParcel.dtd)。我知道如何编写/读取 XML,但不知道如何根据 DTD...有什么区别吗?

有人可以告诉我如何以及最简单的方法吗?我已经在谷歌上找到了好帖子的大部分内容,但从来没有我要找的东西......

谢谢!

ADD #1

注意:我知道 DTD 的用途,我可以使用纯文本编辑器自己创建一个 DTD,并基于 DTD 构建 XML,但我真正的意思是,有没有一种方法可以利用 C# 中的 DTD(创建对象或其他内容...)

ADD #2 附加组件:你们中有人已经设置了一个应用程序来使用 webresque 与加拿大邮政 API 进行对话吗?因为我已经臭了!我用我的数据发送请求,但它永远不会完成,所以永远不会返回响应......这是我的代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给不了的爱 2024-09-19 14:47:36

不,编写 XML 的方式没有什么区别,只是您应该遵守 DTD 中规定的规则。理解和阅读 DTD 是一门艺术,因此我希望加拿大邮政能够以更具描述性的方式向您解释该格式,以帮助您创建正确的 XML。

然后,根据加拿大邮政的要求,您应该根据 DTD 验证您的 XML。虽然有效并不意味着输入是正确的,但它应该尽早警告您有关无效输入的信息。这正是他们希望您这样做的原因:如果根据 DTD 保证您的输出正确,他们可以向您保证他们可以处理输入(至少在大多数情况下)。

下面介绍了如何在 Microsoft 支持上使用 C# 对照 DTD 验证您的输入

关于手动编辑 XML 的注意事项:大多数 XML 编辑器都能够读取 DTD 并警告您 DTD 是否正确,甚至在您键入时(即在 Visual Studio 中)为您提供语法帮助。 XML 标准要求,如果 DTD 出现在 XML 标头中,则 XML 本身必须进行验证,并且如果对 DTD 无效,则不得进行处理。

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.

可是我不能没有你 2024-09-19 14:47:36

您需要创建一个验证 XML 读取器。您需要一个 XmlSchemaSet 来存储架构,并且需要一个 XmlReaderSettings 对象来设置 XmlReader 的配置选项。类似于(未经测试):

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

在 ValidationHandler 中,您可以执行诸如捕获您可能有兴趣输出的任何验证错误/警告之类的操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文