C# 使用数据集表创建xml

C# 使用数据集表创建xml,c#,xml,datatable,xsd,dataset,C#,Xml,Datatable,Xsd,Dataset,我已经创建了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:ele

我已经创建了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>
然后我表演:

a.getXml()
-结果:

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

100
正如您所看到的,它完全忽略模式关系-在模式中,您可以看到childItem上面的每个父元素都是必需的,因此如果我向最深的子元素添加值,我希望xml如下:

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

100
我是否遗漏了什么,或者这是数据集的标准行为?谢谢
我使用的是c#和net4.0,winforms,这是数据集结构;除非遵循层次结构并适当地提供ID,否则将无法获得所需的输出。这就是为什么您没有看到扩展实体的原因,以防您想到它

由于只插入100,其中表的结构是两列,因此child_Id的值为空。该列允许空值,因此插入将通过,因为空值满足外键约束

若要检查,请执行以下操作:

 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.

然后,问题似乎是该工具添加的引用完整性列允许null-没有任何选项可以克服这种行为。

mySchema看起来像什么?你的模式在文件中吗?mySchema是schema在文件中吗?不,我的意思是我想看看你是如何定义变量mySchema的。是小溪吗?文件名的字符串路径?xmlReader?DataSet.EnforceConstraints已启用。在我的模式中,我定义了minoccures——这是否意味着它们必须作为父项包含(例如ParentItem)?如果我尝试验证上面针对mySchema编写的DataSet.getXml()结果,它也无效。因此,我假设DataSet.ReadXmlSchema更改了模式,然后基于此更改的模式创建xml
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.