使用WriteXml方法序列化问题时, [英] Serialization Issue when using WriteXML method

查看:122
本文介绍了使用WriteXml方法序列化问题时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用代码做的是出口数据集到XML

What I'm trying to do with the code is to export a dataset to XML.

这是我目前使用的:

dataSet.WriteXml(fileDialog.FileName, XmlWriteMode.WriteSchema);



我的数据是正确形成了类型化的DataSet(我的意思是,所有的表都PK和FK关系是在数据集中的所有现有表)之间。有些关系是嵌套关系。该表TABLE有两个FK,并在同一时间是父母给其他8表

My dataSet is a typed dataset properly formed (by this I mean, all tables have PK, and FK relations are set between all existing tables in the dataSet). Some relationships are nested relationships. The table "TABLE" has two FK and at the same time is parent to other 8 tables.

我收到以下错误:不能与序列化数据表进行TABLE,它包含了对同一外键的多个父行一个DataRow。

中央社任何人给我一些指点对我有什么做错了什么?为什么我得到这个错误讯息?

Cna anyone give me some pointers on what I'm doing wrong? and why I'm getting this error message?

感谢。

推荐答案

我知道这是一个有点晚了,但我已经找到了解决办法。

I know it's a bit late, but I have found a workaround.

我遇到了同样的问题,尝试读取架构到有数据集关系。你会在这种情况下得到的错误是:
'同桌'{0}'不能是两个嵌套关系中的子表'
我将分享我所学到

I ran into the same problem while trying to read a schema into a dataset that has the relations. The error you will get in that case is: 'The same table '{0}' cannot be the child table in two nested relations' I will share what I have learned

该数据集有两种操作模式,虽然不能从外部讲这个。

The dataset operates in two modes, though you CAN NOT tell this from the outside.


  1. (一)我是一个严格的/手动创建的数据集,不喜欢的嵌套关系

  2. (二)我是一个序列化对象的容器中,一切顺利

您所创建的数据目前是一个'一',我们要使它成为一个b。 $ B哪种模式它是'装'一DatSet当决定操作$ B(XML)和或其他一些注意事项。

The dataset you have created is currently an 'a', we want to make it a 'b'. Which mode it operates in is decided when a DatSet is 'loaded' (xml) and or some other considerations.

我花发烧时间阅读的代码DataSet中找出一种方法来欺骗它,我发现,MS可以只添加的数据集和一些额外的检查属性的解决这个问题。结帐的的DataRelation的源代码:的http:// referencesource.microsoft.com/#System.Data/System/Data/DataRelation.cs,d2d504fafd36cd26,references 的,我们需要愚弄的唯一方法是ValidateMultipleNestedRelations'方法。)

I spend feverish hours reading the code of the DataSet to figure out a way to fool it, and I found that MS can fix the problem with just the addition of a property on the dataset and a few additional checks. Checkout the source code for the DataRelation: http://referencesource.microsoft.com/#System.Data/System/Data/DataRelation.cs,d2d504fafd36cd26,references and that the only method we need to fool is the 'ValidateMultipleNestedRelations' method.)

诀窍是数据集误以为它建立所有关系本身。唯一的办法,我发现这样做是真正使数据集创建它们,通过系列化。

The trick is to fool the dataset into thinking it build all relationships itself. The only way I found to do that is to actually make the dataset create them, by using serialization.

(我们使用的是在oursystem哪里我们'部分该解决方案

(We are using this solution in the part of oursystem where we're creating output with a DataSet oriented 3rd party product.)

在元,你想要做的是:


  1. 创建代码数据集,包括关系。尝试,如果你
    可以模仿MS命名约定(尽管不知道如果需要的话)

  2. 序列化数据集(最好有没有它的任何行)

  3. 请序列化数据集的样子MS序列化它。 (我将
    在此下方展开)

  4. 读取数据修改成一个新的实例。

  5. 现在,您可以导入行,MS不检查的关系,
    ,事情应该工作。

  1. Create your dataset in code, including relationships. Try if you can mimic the MS naming convention (though not sure if required)
  2. Serialize your dataset (best to have not any rows in it)
  3. Make the serialized dataset look like MS serialized it. (I'll expand on this below)
  4. Read the modified dataset into a new instance.
  5. Now you can import your rows, MS does not check the relationships, and things should work.

一些实验告诉我,在这种情况下,少更多。
如果一个DataSet读取模式,并且发现没有关系或钥匙列,它将工作在模式'B',否则它会工作在模式'A'。
这可能是可能的,我们仍然可以得到一些关系或Key-列一个B模式下的数据集,但是这是不相关的我们的问题。

Some experimentation taught me that in this situation, less is more. If a DataSet reads a schema, and finds NO relationships or Key-Columns, it will operate in mode 'b' otherwise it will work in mode 'a'. It COULD be possible that we can still get a 'b' mode dataset with SOME relationships or Key-Columns, but this was not pertinent for our problem.

所以,在这里我们去,这个代码假定你有一个扩展方法'序列化',知道如何处理的数据集。

So, here we go, this code assumes you have an extension method 'Serialize' that knows how to handle a dataset.

假设sourceDataSet是带有架构的数据集只。
目标将是实际可用的数据集:

Assume sourceDataSet is the DataSet with the schema only. Target will be the actually usable dataset:

var sourceDataSet = new DataSet();
var source = sourceDataSet.Serialize();
// todo: create the structure of your dataset.
var endTagKeyColumn = " msdata:AutoIncrement=\"true\" type=\"xs:int\" msdata:AllowDBNull=\"false\" use=\"prohibited\" /";
var endTagKeyColumnLength = endTagKeyColumn.Length - 1;

var startTagConstraint = "<xs:unique ";
var endTagConstraint = "</xs:unique>";
var endTagConstraintLength = endTagConstraint.Length - 1;

var cleanedUp = new StringBuilder();
var subStringStart = 0;
var subStringEnd = source.IndexOf(endTagKeyColumn);

while (subStringEnd > 0)
{
    // throw away unused key columns.
    while (source[subStringEnd] != '<') subStringEnd--;
    if (subStringEnd - subStringStart > 5)
    {
        cleanedUp.Append(source.Substring(subStringStart, subStringEnd - subStringStart));
    }
    subStringStart = source.IndexOf('>', subStringEnd + endTagKeyColumnLength) + 1;
    subStringEnd = source.IndexOf(endTagKeyColumn, subStringStart);
}

subStringEnd = source.IndexOf(startTagConstraint, subStringStart);
while (subStringEnd > 0)
{
    // throw away relationships.
    if (subStringEnd - subStringStart > 5)
    {
        cleanedUp.Append(source.Substring(subStringStart, subStringEnd - subStringStart));
    }
    subStringStart = source.IndexOf(endTagConstraint, subStringEnd) + endTagConstraintLength;
    subStringEnd = source.IndexOf(startTagConstraint, subStringStart);
}
cleanedUp.Append(source.Substring(subStringStart + 1));
target = new DataSet();
using (var reader = new StringReader(cleanedUp.ToString()))
{
    target.EnforceConstraints = false;
    target.ReadXml(reader, XmlReadMode.Auto);
}

请注意,这样我在一开始说,我必须解决这个问题当我们正在加载的数据集,虽然您要保存数据集,解决方法是一样的。

Note, so as I said at the start, I had to fix this problem when we are loading the dataset, and though you are saving the dataset, the workaround will be the same.

这篇关于使用WriteXml方法序列化问题时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆