微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 使用DataSet表创建xml

我创建了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

解决方法

这是DataSet结构;除非您遵循层次结构,并且提供了相应的ID,否则您将无法获得所需的输出.有 also a reason为什么你没有看到扩展实体,以防你想到它.

由于您只插入100,表的结构是两列,因此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.

然后问题似乎是该工具添加的参照完整性列允许为null – 没有选项可以克服该行为.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐