How can I get an XmlNode from a DataSet?
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>开发者_JS百科</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?
I'd suggest doing exactly that. Load it into an XmlDocument, process it, copy it over.
精彩评论