如何在 C# 类中最好地捕获此 XML 架构

发布于 2024-09-18 01:55:18 字数 1297 浏览 4 评论 0原文

我有一个大型XML架构< /a> 具有如下所示的元素:

<xs:element name="Tmats">
    <xs:complexType>
        <xs:sequence>

            <xs:element name="ProgramName" type="xs:string" minOccurs="0">
                <xs:annotation>
                    <xs:documentation>PN</xs:documentation> 
                </xs:annotation>
            </xs:element>

            <xs:element name="TestItem" type="xs:string" minOccurs="0">
                <xs:annotation>
                    <xs:documentation>TA</xs:documentation> 
                </xs:annotation>
            </xs:element>

为了有效地使用它,我需要元素 name 属性和 documentation 元素之间的关联,如下所示

TestItem <==> TA

:我的想法是,这些元素应该具有捕获文档元素的属性,如下所示:

public partial class Tmats
{
    [Documentation("PN")]
    public string ProgramName { get; set; }

    [Documentation("TA")]
    public string TestItem { get; set; }
}

...但我担心性能,因为在正常使用期间这些属性将被广泛扫描。

我第一次尝试使用 XSD.EXE 创建 C# 类,但该工具似乎根本无法捕获注释元素。另外,它创建的代码非常丑陋。

建议?有更好的方法来解决这个问题吗?

I have a large XML schema that has elements that look like this:

<xs:element name="Tmats">
    <xs:complexType>
        <xs:sequence>

            <xs:element name="ProgramName" type="xs:string" minOccurs="0">
                <xs:annotation>
                    <xs:documentation>PN</xs:documentation> 
                </xs:annotation>
            </xs:element>

            <xs:element name="TestItem" type="xs:string" minOccurs="0">
                <xs:annotation>
                    <xs:documentation>TA</xs:documentation> 
                </xs:annotation>
            </xs:element>

To use it effectively, I need an association between the element name attribute, and the documentation element, as in:

TestItem <==> TA

My first thought was that the elements should have attributes to capture the documentation elements, like this:

public partial class Tmats
{
    [Documentation("PN")]
    public string ProgramName { get; set; }

    [Documentation("TA")]
    public string TestItem { get; set; }
}

...but I am concerned about performance, as these attributes will be scanned pretty extensively during normal usage.

I took a first stab at creating C# classes using XSD.EXE, but that tool doesn't appear to capture the annotation elements at all. Plus, the code it creates is pretty ugly.

Suggestions? Is there a better way to approach this?

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

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

发布评论

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

评论(2

丶视觉 2024-09-25 01:55:21

很难回答的问题。你说模式很大。您经常预计它会发生变化吗?手动编码 C# 版本可能很乏味,但如果预计不会发生太大变化,那么您将获得所需的界面,因为您是编码的人。但是,如果它有机会频繁更改,则需要频繁更新您的映射和广泛的单元测试套件,以确保您不会破坏更新中的任何内容。

我过去所做的就是使用 XSD 工具生成初始代码,然后从生成的类进行扩展,或者使其成为部分类以根据我的喜好修复接口。无论如何都不理想,但它使我能够生成大部分代码,是的,丑陋,但生成,并定制我关心的接口,使它们更加友好,并添加任何可能的错误/边界检查有用的。

Tough question. You say the schema is large. Hoe often do you anticipate it changing? Hand coding the C# version can be tedious, but if it is not anticipated to change much, then you will get the interface you desire as you are the one coding it. However, if it has the opportunity to change frequently, that will require frequent updating to you mapping and an extensive unit test suite to ensure you do not break anything in your updates.

What I have done in the past is to use the XSD tool to generate the initial code, and then either extend from the generated classes, or make it a partial class to fix the interface to my liking. Not ideal by any mean, but it gave me the ability to generate most of the code, yes, ugly, but generated, and tailor the interfaces that I cared about to make them a little more friendly and add any error/bounds checking that might be useful.

柠北森屋 2024-09-25 01:55:20

您可能需要编写自己的 xsd.exe 替代品。 AFAIK xsd.exe 不支持 元素。

一个相当不错的博客描述了如何编写自己的基于 xsd 的代码生成器(顺便说一句,它的复杂性中等,并且根据我的个人经验,不需要太通用地构建它):

http://blogs.rev-net .com/ddewinter/2008/09/28/generate-serialization-classes-as-part-of-your-build-part-2/

注意:在博客条目中 ; 节点用于通过 CodeDOM 生成文档。当然,您可以生成其他更能满足您需求的东西。

You will probably have to write your own xsd.exe replacement. AFAIK xsd.exe doesn't support <xs:annotation> elements.

A fairly decent blog describes how to write your own xsd-based code generator (it's medium complexity, btw, and based on my personal experience there's no need to build it too generically):

http://blogs.rev-net.com/ddewinter/2008/09/28/generate-serialization-classes-as-part-of-your-build-part-2/

Note: in the blog entry the <xs:annotation> node is consumed to generate documentation via CodeDOM. Naturally you can generate something else which better meets your needs.

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