XML 模式:特定标签内或单个元素内的集合?

发布于 2024-11-17 14:30:30 字数 1346 浏览 3 评论 0原文

在编写我的第一个复杂的 XML 模式时,我可能仍在考虑面向对象,并且不确定最佳实践是什么。这主要是因为我没有花太多时间在代码中读取 XML 文件。

我倾向于做的一件事是将所有集合放入它自己的元素中。让我们设想一个场景。我正在为一个人类型做一个模式,我不想保留有关人的汽车的信息以及其他信息。

我总是这样做:

<xs:element name="Person">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Name" type="xs:string"/>
            <xs:element name="Address" type="xs:string"/>
            <xs:element name="Cars">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="Car" type="CarType" maxOccurs="unbounded" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

但据说这是可能的,甚至可能更好:

<xs:element name="Person">
    <xs:complexType>
        <xs:sequence>
          <xs:element name="Name" type="xs:string"/>
          <xs:element name="Address" type="xs:string"/>
          <xs:element name="Cars" type="CarType" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

当我使用 C# 和/或 Linq-to-XML 读取 XML 文件时,哪种方法更好?

我倾向于做我的事情,因为我觉得一个集合应该位于它自己的标签内,而不是与名称和地址等单一属性一起放置。

Writing my first complex XML schema I'm perhaps still thinking to object oriented and not sure what the best practices are. This is mainly because I haven't spent much time reading from XML files in code.

One thing I tend to do is putting all collections inside it's own element. Let's come up with a scenario. I'm doing a schema for a person type and I wan't to keep info about the persons cars among other things.

I always do this:

<xs:element name="Person">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Name" type="xs:string"/>
            <xs:element name="Address" type="xs:string"/>
            <xs:element name="Cars">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="Car" type="CarType" maxOccurs="unbounded" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

But supposedly this is possible and perhaps even better:

<xs:element name="Person">
    <xs:complexType>
        <xs:sequence>
          <xs:element name="Name" type="xs:string"/>
          <xs:element name="Address" type="xs:string"/>
          <xs:element name="Cars" type="CarType" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

When I read from a XML file using C# and/or Linq-to-XML which method is better?

I tend to do my thing because I feel like a collection should sit inside it's own tags instead of along with the properties that are singular like name and address.

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

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

发布评论

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

评论(1

农村范ル 2024-11-24 14:30:30

使用 LINQ to XML 时,您可以说将元素直接放在 Person 节点下会更好。 但我个人也更喜欢特殊的集合元素相比

person.Elements("Car")

person.Element("Cars").Elements()

,我认为它更干净。为什么你认为另一种方式更好?谁说的?

When using LINQ to XML, you could say that having the elements directly under the Person node would be better. Compare

person.Elements("Car")

with

person.Element("Cars").Elements()

But I would personally prefer special collection element too, I think it's cleaner. Why do you think the other way is supposedly better? Who said that?

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