Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have 2 xml files as follows:
student.xml
XML
<?xml version="1.0" encoding="utf-8" ?>
<StudentTable>
    <Student>
        <StudentID>1</StudentID>
        <ClassID>1</ClassID>
        <FirstName>James</FirstName>
        <Marks>82.65</Marks>
    </Student>
</StudentTable>


classes.xml
XML
<?xml version="1.0" encoding="utf-8" ?>
<ClassesTable>
    <Classes>
        <ClassID>1</ClassID>
        <ClassNo>Classroom 1</ClassNo>
    </Classes>
    <Classes>
        <ClassID>2</ClassID>
        <ClassNo>Classroom 2</ClassNo>
    </Classes>
</ClassesTable>


and the mainDataSet.xsd file
XML
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="mainDataSet" targetNamespace="http://tempuri.org/mainDataSet.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/mainDataSet.xsd" xmlns:mstns="http://tempuri.org/mainDataSet.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Classes">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="ClassID" type="xs:integer" minOccurs="0" />
                <xs:element name="ClassNo" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
        <xs:key name="ClassID">
            <xs:selector xpath="." />
            <xs:field xpath="mstns:ClassID" />
        </xs:key>
    </xs:element>
    <xs:element name="Student">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="StudentID" type="xs:integer" />
                <xs:element name="FirstName" type="xs:string" />
                <xs:element name="ClassID" type="xs:integer" />
                <xs:element name="Marks" type="xs:double" />
            </xs:sequence>
        </xs:complexType>
        <xs:key name="StudentID">
            <xs:selector xpath="." />
            <xs:field xpath="mstns:StudentID" />
        </xs:key>
        <xs:keyref name="ClassesStudent" refer="ClassID">
            <xs:selector xpath="." />
            <xs:field xpath="mstns:ClassID" />
        </xs:keyref>
    </xs:element>
</xs:schema>


The code below saves a new student to the student.xml file.

VB
Dim newStudent As DataRow = Me.myDataSet.Tables("Student").NewRow

newStudent("StudentID") = Me.StudentIDTextBox.Text
newStudent("FirstName") = Me.FirstNameTextBox.Text
newStudent("ClassID") = Me.ClassIDComboBox.SelectedValue
newStudent("Marks") = Me.MarksTextBox.Text
Me.myDataSet.Tables("Student").Rows.Add(newStudent)
Me.myDataSet.WriteXml(Application.StartupPath & "\AppData\Students.xml", XmlWriteMode.IgnoreSchema)


I, however, have these problems:
1. when a student is added, irrespective of the XMLWriteMode that I use, the new student.xml file ends up with a copy of the classes.xml file in it as well, each time. (the reference to my xsd file is added, too, but I have no problem with that).
student.xml (after adding new student)
XML
<?xml version="1.0" standalone="yes"?>
<mainDataSet xmlns="http://tempuri.org/mainDataSet.xsd">
  <Classes>
    <ClassID>1</ClassID>
    <ClassNo>Classroom 1</ClassNo>
  </Classes>
  <Classes>
    <ClassID>2</ClassID>
    <ClassNo>Classroom 2</ClassNo>
  </Classes>
  <Classes>
    <ClassID>1</ClassID>
    <ClassNo>Classroom 1</ClassNo>
  </Classes>
  <Classes>
    <ClassID>2</ClassID>
    <ClassNo>Classroom 2</ClassNo>
  </Classes>
  <Student>
    <StudentID>5</StudentID>
    <FirstName>James</FirstName>
    <ClassID>3</ClassID>
    <Marks>82.65</Marks>
  </Student>
  <Student>
    <StudentID>2</StudentID>
    <FirstName>Jamesm</FirstName>
    <ClassID>1</ClassID>
    <Marks>82.65</Marks>
  </Student>
  <Student>
    <StudentID>2</StudentID>
    <FirstName>Jamesm</FirstName>
    <ClassID>1</ClassID>
    <Marks>82.65</Marks>
  </Student>
</mainDataSet>



I would like to know why it replicates the parent xml content into this xml file and how to stop it?

2. Does XML support autoincrement? How can it be implemented in my code above?
3. Does XML support referential integrity? eg, I would like to have, say when you delete student with ID 2, the next insertion should not add with ID 2, but jump to ID 3.
Posted
Updated 20-Dec-10 17:39pm
v2

1 solution

1) Your main mainDataSet.xsd describes both the element Classes and Student. This isn't somekind of or instruction so it replicates both elements.

2) with XSLT it is possible to use the function generate-id() to generate unique id's. But normally you would make it the responsibility of your database engine.

3) No it doesn't. It's just a representation of the data. A database engine is responsible for the data integrity. As you can see you can have multiple Student elements and that's fine. It would make everything very hard by the way if you would have all those rules defined everywhere. You would end up with a system where every part is responsible for everything.

Good luck!
 
Share this answer
 
Comments
#realJSOP 26-Dec-10 9:41am    
Good answer. 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900