ds.WriteXml(strXmlTestCasePath, XmlWriteMode.IgnoreSchema);
ds is a dataset
. I want to add an extra line or extra information into this XML. How do I do it?
ds是一个数据集。我想在这个XML中添加额外的行或额外的信息。我该怎么做呢?
2 个解决方案
#1
1
You can't simply write more XML to the end of a serialized DataSet
, since if you do you'll be producing an XML document with more than one top-level element. Using an XmlWriter
, you'd need to do something like this:
不能简单地将更多的XML写到序列化数据集的末尾,因为如果这样做,您将生成一个包含多个*元素的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();
}
But this won't help you if what you're trying to do is add XML elements inside the serialized DataSet
. To do that, you'll need to serialize the DataSet
, read it into an XmlDocument
, and then use DOM methods to manipulate the XML.
但是,如果您要做的是在序列化的数据集中添加XML元素,那么这对您没有帮助。为此,需要序列化数据集,将其读入XmlDocument,然后使用DOM方法操作XML。
Or, alternatively, create and populate a new DataTable
right before you serialize the DataSet
and then delete it when you're done. It really depends on what your actual requirements are.
或者,在序列化数据集之前创建并填充一个新的数据表,然后在完成之后删除它。这取决于你的实际需求是什么。
#2
5
Use an XmlWriter
to write your DataSet
. You can then use the same object to write additional XML.
使用XmlWriter来编写数据集。然后,您可以使用相同的对象来编写附加的XML。
illustrative code:
说明代码:
System.Data.DataSet ds;
System.Xml.XmlWriter x;
ds.WriteXml(x);
x.WriteElementString("test", "value");
#1
1
You can't simply write more XML to the end of a serialized DataSet
, since if you do you'll be producing an XML document with more than one top-level element. Using an XmlWriter
, you'd need to do something like this:
不能简单地将更多的XML写到序列化数据集的末尾,因为如果这样做,您将生成一个包含多个*元素的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();
}
But this won't help you if what you're trying to do is add XML elements inside the serialized DataSet
. To do that, you'll need to serialize the DataSet
, read it into an XmlDocument
, and then use DOM methods to manipulate the XML.
但是,如果您要做的是在序列化的数据集中添加XML元素,那么这对您没有帮助。为此,需要序列化数据集,将其读入XmlDocument,然后使用DOM方法操作XML。
Or, alternatively, create and populate a new DataTable
right before you serialize the DataSet
and then delete it when you're done. It really depends on what your actual requirements are.
或者,在序列化数据集之前创建并填充一个新的数据表,然后在完成之后删除它。这取决于你的实际需求是什么。
#2
5
Use an XmlWriter
to write your DataSet
. You can then use the same object to write additional XML.
使用XmlWriter来编写数据集。然后,您可以使用相同的对象来编写附加的XML。
illustrative code:
说明代码:
System.Data.DataSet ds;
System.Xml.XmlWriter x;
ds.WriteXml(x);
x.WriteElementString("test", "value");