【发布时间】:2025-12-07 07:25:01
【问题描述】:
ds.WriteXml(strXmlTestCasePath, XmlWriteMode.IgnoreSchema);
ds 是dataset。我想在这个 XML 中添加额外的一行或额外的信息。我该怎么做?
【问题讨论】:
ds.WriteXml(strXmlTestCasePath, XmlWriteMode.IgnoreSchema);
ds 是dataset。我想在这个 XML 中添加额外的一行或额外的信息。我该怎么做?
【问题讨论】:
使用XmlWriter 写您的DataSet。然后,您可以使用相同的对象来编写其他 XML。
示例代码:
System.Data.DataSet ds;
System.Xml.XmlWriter x;
ds.WriteXml(x);
x.WriteElementString("test", "value");
【讨论】:
您不能简单地将更多 XML 写入序列化的 DataSet 的末尾,因为如果这样做,您将生成一个包含多个*元素的 XML 文档。使用XmlWriter,您需要执行以下操作:
using (XmlWriter xw = XmlWriter.Create(strXmlTestCasePath));
{
xw.WriteStartElement("container");
ds.WriteXml(xw, XmlWriteMode.IgnoreSchema);
// from here on, you can use the XmlWriter to add XML to the end; you then
// have to wrap things up by closing the enclosing "container" element:
...
xw.WriteEndElement();
}
但是,如果您尝试在序列化的DataSet 中在 中添加 XML 元素,这将无济于事。为此,您需要序列化DataSet,将其读入XmlDocument,然后使用DOM 方法来操作XML。
或者,或者,在序列化DataSet 之前创建并填充一个新的DataTable,然后在完成后将其删除。这真的取决于您的实际要求。
【讨论】: