如何在Xlement中获得数据集的分布?(或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似乎很浪费。因此,我试图找出如何填充此代码中的丢失链接,该链接应该传输diffgram的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
我希望找到我的代码问题的答案,甚至可能了解流/读者/作者的真正工作方式。
您需要一个从数据集写入xdocument的作者,而不是读者:
XDocument xd = new XDocument();
using (var writer = xd.CreateWriter())
{
ds.WriteXml(writer, XmlWriteMode.WriteSchema); // XmlWriteMode.DiffGram
}