【问题标题】:"Database" structure in XML fileXML 文件中的“数据库”结构
【发布时间】:2020-09-16 11:24:41
【问题描述】:

所以我必须在必须有学校、成绩、房间、课程和学生的 xml 文档中定义一个“数据库”,然后我必须用 C# 编写一些必须获取数据并将数据保存到 xml 的类文件在反序列化和序列化的帮助下。 但问题是我不知道应该如何构建 XML 文件,现在我的文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<School>
   <Grades>

   </Grades>
   <Rooms>

   </Rooms>
   <Courses>

   </Courses>
   <Students>

   </Students>
</School>

但我一直想知道这是否是更好的方法:

<?xml version="1.0" encoding="utf-8" ?>
<School>
   <Grades>
      <Rooms>
         <Courses>
            <Students>

            </Students>
         </Courses>
      </Rooms>
   </Grades>
</School>

或者也许我忽略了第三种方式,任何帮助将不胜感激

【问题讨论】:

    标签: xml xslt xsd xml-serialization xml-deserialization


    【解决方案1】:

    这个问题是基于意见的。所以,这只是我的看法:

    选择第一个布局并使用属性@IDs 在它们之间链接。
    例如:

    <?xml version="1.0" encoding="utf-8" ?>
    <School>
       <Grades>
         <Grade id="gr1">...</Grade>
       </Grades>
       <Rooms>
         <Room id="rm1">...</Room>    
       </Rooms>
       <Courses>
         <Course id="crs1">
           <RoomNr>rm1</RoomNr>  <!-- Referring to the Rooms/Room/@id rm1 -->
           ...
         </Course>
       </Courses>
       <Students>
         <Student id="1">
           <Course>crs1</Course> <!-- Referring to the Courses/Course/@id crs1 -->
           <Course>...</Course>
         </Student>
       </Students>
    </School>
    

    此布局易于查询,更改链接不会显着改变布局。

    例如(因为您用 标记了您的问题):

    • 可以使用 XPath-1.0 表达式检索所有 &lt;Student&gt;s

      /School/Students/Student
      
    • 检索访问某个课程的所有&lt;Student&gt;s(例如crs1)

      /School/Students/Student[Course='crs1']
      

    ...等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 2012-02-28
      相关资源
      最近更新 更多