1.将XML报文转化为DataSet
DataSet dsWareHouse
=
new
DataSet();
StringReader srr = new StringReader(XmlSource);
dsWareHouse.ReadXml(srr);
StringReader srr = new StringReader(XmlSource);
dsWareHouse.ReadXml(srr);
2.将DataSet转化为XML报文
1.直接进行写出dsWareHouse.WriteXml(“D:\\a.xml”);
2.进行编排再导出
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#region
GetQueryProcessInfo
private string GetQueryProcessInfo(System.Data.DataSet ds)
{
if (ds == null || ds.Tables.Count <= 0 || ds.Tables[ 0 ].Rows.Count <= 0 )
return string .Empty;
ds.Tables[ 0 ].TableName = " STEP " ;
string Remark = string .Empty;
if (ds.Tables[ 0 ].Columns.Contains( " REMARK " ))
{
Remark = ds.Tables[ 0 ].Rows[ 0 ][ " REMARK " ].ToString();
ds.Tables[ 0 ].Columns.Remove( " REMARK " );
}
System.Data.DataSet dsHeader = new DataSet();
System.Data.DataTable dtheader = new DataTable();
dtheader.TableName = " RESPONSEPROCESS " ;
dtheader.Columns.Add( " SUCCESS " , typeof ( string ));
dtheader.Columns.Add( " REMARK " , typeof ( string ));
System.Data.DataRow drHeader = dtheader.NewRow();
drHeader.BeginEdit();
drHeader[ " SUCCESS " ] = " TRUE " ;
drHeader[ " REMARK " ] = Remark;
drHeader.EndEdit();
dtheader.Rows.Add(drHeader);
dsHeader.Tables.Add(dtheader);
dsHeader.EnforceConstraints = false ;
ds.EnforceConstraints = false ;
XmlDataDocument doc = new XmlDataDocument(dsHeader);
XmlElement newElem = doc.CreateElement( " STEPS " );
XmlDataDocument datadoc = new XmlDataDocument(ds);
newElem.InnerXml += datadoc.DocumentElement.InnerXml;
doc.DocumentElement.ChildNodes[ 0 ].AppendChild(newElem);
doc.DataSet.AcceptChanges();
return doc.ChildNodes[ 0 ].InnerXml;
}
#endregion
private string GetQueryProcessInfo(System.Data.DataSet ds)
{
if (ds == null || ds.Tables.Count <= 0 || ds.Tables[ 0 ].Rows.Count <= 0 )
return string .Empty;
ds.Tables[ 0 ].TableName = " STEP " ;
string Remark = string .Empty;
if (ds.Tables[ 0 ].Columns.Contains( " REMARK " ))
{
Remark = ds.Tables[ 0 ].Rows[ 0 ][ " REMARK " ].ToString();
ds.Tables[ 0 ].Columns.Remove( " REMARK " );
}
System.Data.DataSet dsHeader = new DataSet();
System.Data.DataTable dtheader = new DataTable();
dtheader.TableName = " RESPONSEPROCESS " ;
dtheader.Columns.Add( " SUCCESS " , typeof ( string ));
dtheader.Columns.Add( " REMARK " , typeof ( string ));
System.Data.DataRow drHeader = dtheader.NewRow();
drHeader.BeginEdit();
drHeader[ " SUCCESS " ] = " TRUE " ;
drHeader[ " REMARK " ] = Remark;
drHeader.EndEdit();
dtheader.Rows.Add(drHeader);
dsHeader.Tables.Add(dtheader);
dsHeader.EnforceConstraints = false ;
ds.EnforceConstraints = false ;
XmlDataDocument doc = new XmlDataDocument(dsHeader);
XmlElement newElem = doc.CreateElement( " STEPS " );
XmlDataDocument datadoc = new XmlDataDocument(ds);
newElem.InnerXml += datadoc.DocumentElement.InnerXml;
doc.DocumentElement.ChildNodes[ 0 ].AppendChild(newElem);
doc.DataSet.AcceptChanges();
return doc.ChildNodes[ 0 ].InnerXml;
}
#endregion
得出结果大概如下:
< RESPONSEPROCESS >
< SUCCESS >True</ SUCCESS >
< REMARK >示例</ REMARK >
< STEPS >
< STEP >
<>
<一堆DS里的信息>
<>
< /STEP >
< /STEPS >
</ RESPONSEPROCESS >
3.这个方法好
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#region
ConstrutorXmlString
private string ConstrutorXmlString(DataSet ds)
{
DataTable dtResopnse = ds.Tables[ 0 ];
DataTable dtStep = ds.Tables[ 1 ];
string xmlString = " <ResponseProcess><success> " + dtResopnse.Rows[ 0 ][ " SUCCESS " ].ToString() + " </success><steps> " ;
foreach (DataRow dr in dtStep.Rows)
{
xmlString += " <step><seq> " + dr[ " SEQ " ].ToString() + " </seq> " +
" <operateTime> " + dr[ " OPERATE_TIME " ].ToString() + " </operateTime> " +
" <operateContent> " + dr[ " OPERATE_CONTENT " ].ToString() + " </operateContent> " +
" <operator> " + dr[ " OPERATOR " ].ToString() + " </operator></step> " ;
}
xmlString += " </steps><remark> " + dtResopnse.Rows[ 0 ][ " REMARK " ].ToString() + " </remark></ResponseProcess> " ;
return xmlString;
}
#endregion
private string ConstrutorXmlString(DataSet ds)
{
DataTable dtResopnse = ds.Tables[ 0 ];
DataTable dtStep = ds.Tables[ 1 ];
string xmlString = " <ResponseProcess><success> " + dtResopnse.Rows[ 0 ][ " SUCCESS " ].ToString() + " </success><steps> " ;
foreach (DataRow dr in dtStep.Rows)
{
xmlString += " <step><seq> " + dr[ " SEQ " ].ToString() + " </seq> " +
" <operateTime> " + dr[ " OPERATE_TIME " ].ToString() + " </operateTime> " +
" <operateContent> " + dr[ " OPERATE_CONTENT " ].ToString() + " </operateContent> " +
" <operator> " + dr[ " OPERATOR " ].ToString() + " </operator></step> " ;
}
xmlString += " </steps><remark> " + dtResopnse.Rows[ 0 ][ " REMARK " ].ToString() + " </remark></ResponseProcess> " ;
return xmlString;
}
#endregion