Creating xml with DataSet tables

I created 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> 

I want to load this schema into a DataSet and then edit and create xml

so I'm trying to populate the childItem element with the value 100:

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

then I do:

a.getXml() - result:

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

As you can see it completely ignores the relationship of the scheme - in the scheme you can see that every parent element above the childItem is required, therefore, if I add a value to the deepest children, I expect xml to like:

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

Am I missing something, or is this the standard behavior of a DataSet? thank you very much I am using C # and net4.0, winforms

+4
source share
1 answer

This is a DataSet structure; if you do not follow the hierarchy and provide identifiers accordingly, you will not get the desired result. There is also a reason why you do not see the Extension object if you thought about it.

enter image description here

Since you only insert 100 , where the table structure is two columns, you get NULL for child_Id. The column is nullable, so insertion passes because a null value satisfies the foreign key constraint.

To check, follow these steps:

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

before your addition you will see this error:

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

If you then do:

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

You are getting:

 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. 

Then the problem is that the referential integrity columns added by the tool are null - there is no way to overcome this behavior.

+1
source

Source: https://habr.com/ru/post/1447611/


All Articles