4

xsd を作成しました:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="test" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Extension">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="parent">
          <xs:annotation>
            <xs:documentation></xs:documentation>
          </xs:annotation>
          <xs:complexType>
            <xs:sequence>
              <xs:element minOccurs="1" maxOccurs="unbounded" name="parentItem">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="child">
                      <xs:annotation>
                        <xs:documentation></xs:documentation>
                      </xs:annotation>
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element minOccurs="1" maxOccurs="unbounded" default="10" name="childItem" type="xs:integer" />
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

このスキーマを DataSet に読み込み、xml を編集して作成したい

だから私は値100でchildItem要素を埋めようとします:

  DataSet a = new DataSet();
  a.ReadXmlSchema(mySchema);
  a.Tables[3].Rows.Add(100);

それから私は実行します:

a.getXml()- 結果:

<Extension xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="test">
  <childItem xmlns="">100</childItem>
</Extension>

ご覧のとおり、スキーマ関係は完全に無視されます。スキーマでは、childItem の上のすべての親要素が必要であることがわかります。そのため、最も深い子要素に値を追加すると、次のような xml が期待されます。

<Extension>
   <Parent>
      <ParentItem>
        <Child>
          <ChildItem>100<ChildItem/>
        <Child/>
      <ParentItem/>
   <Parent/>
<Extension/>

何か不足していますか、それとも DataSet の標準的な動作ですか? どうもありがとう、私はc#とnet4.0、winformsを使用しています

4

1 に答える 1

1

これは DataSet 構造です。階層に従って ID を適切に指定しない限り、必要な出力は得られません。念のため、拡張エンティティが表示されない理由もあります。

ここに画像の説明を入力

テーブルの構造が 2 つの列である 100のみを挿入しているため、 child_Idに対して NULL 値が取得されます。列は null を許可するため、null 値は外部キー制約を満たすため、挿入はパスします。

確認するには、次のようにします。

 a.Tables[3].Columns[1].AllowDBNull = false;

Add の前に、次のエラーが表示されます。

Error line 11:      a.Tables[3].Rows.Add(100);
Column 'child_Id' does not allow nulls.

次に行う場合:

a.Tables[3].Rows.Add(100, 0);

あなたは得る:

Error line 11:      a.Tables[3].Rows.Add(100, 0);
ForeignKeyConstraint child_childItem requires the child key values (0) to exist in the parent table.

問題は、ツールによって追加された参照整合性列が null を許可していることにあるようです。その動作を克服するオプションはありません。

于 2012-11-22T00:19:24.277 に答える