創建

2012-11-21 48 views
4

我創建了XSD數據集表的xml:創建

<?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

所以我儘量填滿childItem元素值爲100:

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

+0

mySchema是什麼樣子?你的模式是否在一個文件中? –

+0

mySchema是以上架構 –

+0

不,我的意思是我想看看你如何定義變量mySchema。它是一個流?一個文件名的字符串路徑? XMLReader的? –

回答

1

這是DataSet結構;除非您遵循層次結構並適當提供ID,否則您將無法獲得所需的輸出。有also a reason爲什麼你沒有看到一個分機實體,以防萬一你想到它。

enter image description here

既然你只插入,其中表的結構是兩列,你會得到child_Id NULL值。該列允許空值,所以插入通過,因爲空值滿足外鍵約束。

要檢查,如果你這樣做:

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

您的添加之前,你會看到這樣的錯誤:

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. 

問題似乎是由工具添加的參照完整性列允許爲空 - 沒有選項可以克服該行爲。

+0

DataSet.EnforceConstraints已打開。在我的模式中,我已經定義了minoccures--是不是意味着它們必須被包含爲父類(例如ParentItem)? –

+0

如果我嘗試驗證DataSet.getXml()結果,如上面針對mySchema所寫的那樣,它也不是有效的。所以我想DataSet.ReadXmlSchema更改模式,並根據此更改的模式創建xml –

+0

@MartinCh,請參閱已更新的帖子。 –