XML Schema: Collection inside specific tags or along singular elements?
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 possibl开发者_如何学Goe 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.
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?
精彩评论