XML 模式:特定标签内或单个元素内的集合?
在编写我的第一个复杂的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 LINQ to XML 时,您可以说将元素直接放在
Person
节点下会更好。 但我个人也更喜欢特殊的集合元素相比与
,我认为它更干净。为什么你认为另一种方式更好?谁说的?
When using LINQ to XML, you could say that having the elements directly under the
Person
node would be better. Comparewith
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?