0
我試圖用[的WebMethod]生成一個XML ..如何從一個DataSet顯示分層XML
這是我在MyMethod.asmx.cs的webmethod:
[WebMethod]
public string GetTransactionList(string AccNo)
{
TransactionRepository transRep = new TransactionRepository();
IList<Transaction> listTrans = transRep.GetTransactionList(AccNo);
IList<Parameter> listPara = transRep.GetAllParameter();
DataTable tblTrans = CommonDatatableMethods.ConvertToDataTable<Transaction>(listTrans, true);
tblTrans.TableName = "transaction";
tblTrans.Columns.Remove("SequenceNo");
tblTrans.Columns.Remove("ID");
/** Note: Require insert into dataset to gain custom root name**/
DataSet dsTrans = new DataSet("Transactions");
dsTrans.Tables.Add(tblTrans);
StringWriter sw = new StringWriter();
tblTrans.WriteXml(sw);
if (listTrans.Count <= 0)
{
string emptydata = string.Empty;
emptydata = "<Transactions><transaction>No record for " + AccNo + " within " + listPara[0].ParameterYears + " years data.</transaction></Transactions>";
return emptydata;
}
else
{
/** Note: Need to remove all the schemas in meta tag**/
return sw.ToString().Replace(" xsi:type=\"xs:string\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "").Replace(" xml:space=\"preserve\"", "");
}
}
但是當我點擊按鈕調用上我的web服務,
的XML僅顯示這樣一行:
<Transactions><transaction><AccNo>ABC15279182719</AccNo><FirstName>AHMAD ALI</FirstName></transaction></Transactions>
我要的是什麼層次是這樣的:
<Transactions>
<transaction>
<AccNo>ABC15279182719</AccNo>
<FirstName>AHMAD ALI</FirstName>
</transaction>
</Transactions>
我如何樣的?
謝謝!
您的意思是xml的格式?你試過'XmlWriter'而不是'StringWriter',很確定你可以用'XmlWriter'設置縮進。 –
我還沒試過,btw你是什麼意思縮進?你可以給我一個例子嗎? – CoolTips2u
縮進是你想要的,你的服務返回的xml是完全有效的,你正在尋找用於人類可讀性的縮進xml –