none
xml foreign key (x:keyref ) validation RRS feed

  • Question

  • hi

    pls chk what is the issue on following code

    i am having files "books.xsd"

    <?xml version="1.0" encoding="utf-8"?>
      <xs:schema id="NewDataSet"  xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
          <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
              <xs:element name="product">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="id" type="xs:string" minOccurs="0" />
                    <xs:element name="name" type="xs:string" minOccurs="0" />
                    <xs:element name="price" type="xs:string" minOccurs="0" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="sales">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="salesid" type="xs:string" minOccurs="0" />
                    <xs:element name="productid" type="xs:string" minOccurs="0" />
                    <xs:element name="salesqty" type="xs:string" minOccurs="0" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:choice>
          </xs:complexType>
          <xs:unique name="Constraint1">
            <xs:selector xpath=".//product" />
            <xs:field xpath="id" />
          </xs:unique>
          <xs:keyref name="relation1" refer="Constraint1">
            <xs:selector xpath=".//sales" />
            <xs:field xpath="productid" />
          </xs:keyref>
        </xs:element>
      </xs:schema>

    and product.xml as

    <?xml version="1.0" encoding="utf-8" ?>
    <productdetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="books.xsd">
      <product>
        <id>1</id>
        <name>cinthol</name>
        <price>200</price>
      </product>
      <product>
        <id>2</id>
        <name>liril</name>
        <price>300</price>
      </product>
      <product>
      <id>3</id>
      <name>hammam</name>
      <price>400</price>
      </product>
    </productdetails>

     and sales.xml as

    <?xml version="1.0" encoding="utf-8"?>
    <salesdetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="books.xsd">
      <sales>
        <salesid>1</salesid>
        <productid>1</productid>
        <salesqty>100</salesqty>
      </sales>
      <sales>
        <salesid>2</salesid>
        <productid>2</productid>
        <salesqty>150</salesqty>
      </sales>
      <salesid>3</salesid>
      <productid>3</productid>
      <salesqty>160</salesqty>
    </salesdetails>

    and i added the data in sales xml to check the foriegn key validation(x:keyref validation) as

     try
                {
                    Stream ss = new FileStream(@"sales.xml", FileMode.Append);
                    XmlTextWriter tw = new XmlTextWriter(ss,null);
                    //tw.WriteStartDocument();
                    //tw.WriteStartElement("salesdetails");
                    tw.WriteStartElement("sales");
                    tw.WriteElementString("salesid", "3");
                    tw.WriteElementString("productid", "3");
                    tw.WriteElementString("salesqty", "160");
                    tw.WriteEndElement();
                    //tw.WriteEndElement();
                    //tw.WriteEndDocument();
                     tw.Close();
                }
                catch (Exception ex1)
                {
                    MessageBox.Show(ex1.Message.ToString());
                }

    but the actual foreign key validation not done, pls clear me regarding this


    sidd

    • Moved by Min ZhuModerator Friday, August 24, 2012 7:25 AM (From:Windows Presentation Foundation (WPF))
    Wednesday, August 22, 2012 12:02 PM

All replies