如何从 DataSet 获取 XmlNode?
我正在尝试使用 XmlWriter
和 XmlDocument
编写 XML 文档。我有 4 个子节点要写入根元素,前三个效果很好。然而,最后一个是从 DataSet
生成的。这是我的缩写代码:
DataSetds;
XmlNode RecordSet = xdoc.CreateNode(XmlNodeType.Element, "RecordSet", "");
XmlNode RecordSetTotal = xdoc.CreateNode(XmlNodeType.Attribute, "TOTAL", "");
RecordSetTotal.Value = gvExcelData.Rows.Count.ToString();
RecordSet.Attributes.SetNamedItem(RecordSetTotal);
RecordSet.InnerXml = ds.GetXml();
root.AppendChild(RecordSet);
输出 XML:
<RecordSet TOTAL="2">
<RecordSet>
<Record>
<Column 1></Column 1>
<Column 2></Column 2>
<Column 3></Column 3>
<Column 4></Column 4>
<Column 5></Column 5>
</Record>
<Record>
<Column 1></Column 1>
<Column 2></Column 2>
<Column 3></Column 3>
<Column 4></Column 4>
<Column 5></Column 5>
</Record>
</RecordSet>
</RecordSet>
我只需要一个根元素 RecordSet
并且它需要有一个等于记录总数的属性 Total
。如果我能以某种方式将从 ds.GetXml()
获取的 XML 字符串直接解析为 XmlNode
,那么我就可以设置我的属性并开始工作。但我可能是错的。有什么建议吗?
I'm trying to write an XML document using an XmlWriter
and an XmlDocument
. I have 4 child nodes to write in the root element, and the first three worked out fine. The last one, however, is being generated from a DataSet
. Here is my abbreviated code:
DataSetds;
XmlNode RecordSet = xdoc.CreateNode(XmlNodeType.Element, "RecordSet", "");
XmlNode RecordSetTotal = xdoc.CreateNode(XmlNodeType.Attribute, "TOTAL", "");
RecordSetTotal.Value = gvExcelData.Rows.Count.ToString();
RecordSet.Attributes.SetNamedItem(RecordSetTotal);
RecordSet.InnerXml = ds.GetXml();
root.AppendChild(RecordSet);
Which outputs the XML:
<RecordSet TOTAL="2">
<RecordSet>
<Record>
<Column 1></Column 1>
<Column 2></Column 2>
<Column 3></Column 3>
<Column 4></Column 4>
<Column 5></Column 5>
</Record>
<Record>
<Column 1></Column 1>
<Column 2></Column 2>
<Column 3></Column 3>
<Column 4></Column 4>
<Column 5></Column 5>
</Record>
</RecordSet>
</RecordSet>
I need only one root element RecordSet
and it needs to have an attribute Total
equal to the total number of records. If somehow I could parse the XML string I get from ds.GetXml()
into an XmlNode
directly, I could then set my attributes and be on my way. But I could be wrong. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议就这样做。将其加载到 XmlDocument 中,对其进行处理,然后将其复制过来。
I'd suggest doing exactly that. Load it into an XmlDocument, process it, copy it over.