加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用DataSet表创建xml

发布时间:2020-12-15 21:43:09 所属栏目:百科 来源:网络整理
导读:我创建了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/x
我创建了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 – 没有选项可以克服该行为.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读