如何在XElement中获取DataSet的Diffgram? (或XDocument)
我发现了如何在字符串中获取Diffgram:
// DataSet to Diffgram in a string:
StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 };
using (XmlWriter xw = XmlWriter.Create(sb, settings))
{
ds.WriteXml(xw, XmlWriteMode.WriteSchema);//XmlWriteMode.DiffGram
}
string str = sb.ToString();
但是首先将一个diffgram的xml输出到一个字符串然后再将它解析为一个XElement似乎很浪费。所以我试图找出如何填写此代码中缺少的链接,该链接应该将diff的xml转换为Xml变量而无需转换:
// I have a DataSet filled with some data
DataSet ds = new DataSet();
ds.Tables.Add(ScanData);
// I need the diffgram of the DataSet in an XElement
XElement xe = null;
XmlReader xr = xe.CreateReader();
// I could live with output to XDocument, and extract the XElement later
XDocument xd = new XDocument();
XmlReader xrd = xd.CreateReader();
// Q: How do I construct a stream that connects an XmlReader to ds.WriteXml()?
Stream stream = ...???... ;
// This method creates the DiffGram output format to a stream
ds.WriteXml(stream, XmlWriteMode.WriteSchema);//diffgram output
我希望找到我的代码问题的答案,甚至可以了解流/读者/作者如何真正起作用。
答案 0 :(得分:0)
您需要一个编写器,而不是读者,从DataSet写入XDocument:
XDocument xd = new XDocument();
using (var writer = xd.CreateWriter())
{
ds.WriteXml(writer, XmlWriteMode.WriteSchema); // XmlWriteMode.DiffGram
}