在读取和写入 XML 之间对 DataSet 进行排序
目标是从 XML 填充数据集,对其中一个 DataTable 进行排序,然后将排序后的 DataSet 写回 XML。
我似乎无法对我的数据集进行排序,并且尝试了一些变体。它可以很好地添加数据,但不会排序。希望有人能帮助阻止我拔头发。
DataSet dsXml = new DataSet();
dsXml.ReadXml(msXml);
DataTable tbl;
// Read DataTable.
tbl = dsXml.Tables["cj"];
// Set Primary Key
tbl.PrimaryKey = new DataColumn[] { tbl.Columns["a"] };
DataView dtView = tbl.DefaultView;
DataRow foundRow = tbl.Rows.Find(findCookiejarId);
tbl.Rows.Add(addRow);
dtView.ToTable().Sort = "a ASC";
StringWriter sw = new StringWriter();
dsXml.WriteXml(sw);
更新
现在它会从数据集中删除其他表。 xml 应该是这样的:
<user_data>
<settings>
<a></a>
<b></b>
<c></c>
</settings>
<perms>
<a></a>
<b></b>
<c></c>
</perms>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
</user_data>
它看起来像这样,删除了其他表,但排序工作正常:)
</DocumentElement>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
</DocumentElement>
如何使用 C# 仅对 DataTable cj
进行排序,然后写回整个 DataSet到 XML? settings
和 perms
应保持不变。
The goal is to populate a dataset from XML, sort one of the DataTables, and then write out the sorted DataSet back to XML.
I can't seem to sort my DataSet and have tried a few variations. It adds the data fine but does not sort. Hope someone could help to stop me pulling my hair out.
DataSet dsXml = new DataSet();
dsXml.ReadXml(msXml);
DataTable tbl;
// Read DataTable.
tbl = dsXml.Tables["cj"];
// Set Primary Key
tbl.PrimaryKey = new DataColumn[] { tbl.Columns["a"] };
DataView dtView = tbl.DefaultView;
DataRow foundRow = tbl.Rows.Find(findCookiejarId);
tbl.Rows.Add(addRow);
dtView.ToTable().Sort = "a ASC";
StringWriter sw = new StringWriter();
dsXml.WriteXml(sw);
UPDATE
Now it removes the other tables from the dataset. This is how the xml should look:
<user_data>
<settings>
<a></a>
<b></b>
<c></c>
</settings>
<perms>
<a></a>
<b></b>
<c></c>
</perms>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
</user_data>
And it looks like this, removing the other tables but the sort works fine :)
</DocumentElement>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
</DocumentElement>
How can I use C# to sort just DataTable cj
, and then write the entire DataSet back to XML? The settings
and perms
should remain untouched.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实现目标的一种方法是:
这是在 Pastebin 上排序的概念验证。
我使用了这个带有证明的XML。 输出按您期望的方式显示。
One way to achieve your goal is to:
Here's the proof-of-concept for sorting on pastebin.
I used this XML with the proof. The output is shown sorted as you'd expect.
您已成功将排序应用到 DataView。使用 DataView 的
.ToTable()
方法,然后使用 Table 的.WriteXML()
方法输出到文件。You've successfully applied the sort to the DataView. Use the DataView's
.ToTable()
method, and then use the Table's.WriteXML()
method to output to a file.您创建的 DataView 反映的是排序结果,而不是基础数据集。
The DataView that you created reflects the sorted results, not the underlying dataset.