使用WCF以XML返回DataTable数据 [英] Return DataTable Data in XML Using WCF
本文介绍了使用WCF以XML返回DataTable数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我想从wcf Web服务以XML格式返回大量数据.我使用了返回类型XMLDocument,但IT出现错误:"WCF测试客户端不支持此操作,因为它使用类型为system.object []"
以下是我的代码:
Hi All,
I want to return Large amount of data From wcf web service in XML format. I used return type XMLDocument, but IT is giving error : "This operation is not supported in the WCF Test Client because it uses type system.object[]"
below is my code:
public XmlDocument GetMasterData(double TerritoryCode, double RegionCode, double ZoneCode)
{
SqlConnection conn = new SqlConnection(path_jnj);
SqlCommand cmd;
XmlDocument doc = new XmlDocument();
try
{
cmd = new SqlCommand("GetMastersForAPK", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@TerritoryCode", TerritoryCode);
cmd.Parameters.AddWithValue("@RegionCode", RegionCode);
cmd.Parameters.AddWithValue("@ZoneCode", ZoneCode);
DataTable dtRDS = new DataTable();
SqlDataAdapter daRDS = new SqlDataAdapter();
daRDS.SelectCommand = cmd;
daRDS.Fill(dtRDS);
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec);
XmlElement root = doc.CreateElement("JNJ");
doc.AppendChild(root);
XmlNode chnode1, chnode2, chnode3, chnode4, chnode5, chnode6, chnode7, chnode8, chnode9, chnode10;
foreach (DataRow row in dtRDS.Rows)
{
chnode1 = doc.CreateNode("element", "SAPID", "");
chnode1.InnerText = row[0].ToString();
root = doc.DocumentElement;
root.AppendChild(chnode1);
chnode2 = doc.CreateNode("element", "CustomerName", "");
chnode2.InnerText = row[2].ToString();
root = doc.DocumentElement;
root.AppendChild(chnode2);
chnode3 = doc.CreateNode("element", "SMCode", "");
chnode3.InnerText = row[4].ToString();
root = doc.DocumentElement;
root.AppendChild(chnode3);
chnode4 = doc.CreateNode("element", "SMName", "");
chnode4.InnerText = row[6].ToString();
root = doc.DocumentElement;
root.AppendChild(chnode4);
chnode5 = doc.CreateNode("element", "RMCode", "");
chnode5.InnerText = row[8].ToString();
root = doc.DocumentElement;
root.AppendChild(chnode5);
chnode6 = doc.CreateNode("element", "RMName", "");
chnode6.InnerText = row[10].ToString();
root = doc.DocumentElement;
root.AppendChild(chnode6);
chnode7 = doc.CreateNode("element", "RtrId", "");
chnode7.InnerText = row[12].ToString();
root = doc.DocumentElement;
root.AppendChild(chnode7);
chnode8 = doc.CreateNode("element", "RtrName", "");
chnode8.InnerText = row[14].ToString();
root = doc.DocumentElement;
root.AppendChild(chnode8);
chnode9 = doc.CreateNode("element", "RtrCategoryCode", "");
chnode9.InnerText = row[16].ToString();
root = doc.DocumentElement;
root.AppendChild(chnode9);
chnode10 = doc.CreateNode("element", "ClassDesc", "");
chnode10.InnerText = row[18].ToString();
root = doc.DocumentElement;
root.AppendChild(chnode10);
}
}
catch (FaultException Fex)
{}
return doc;
}
请为我提供解决方案.
在Advance中致谢
please provide me solution.
Thanks in Advance
推荐答案
我建议您以字符串形式返回xml,而不是返回整个XmlDocument.如下图所示
I will recommend you to return xml in a string instead of returning whole XmlDocument. Like below
return doc.OuterXml
在WCF客户端(正在调用WCF服务)上,您可以使用LoadXml方法在XmlDocument中加载xml字符串.像下面的
and on the WCF client side (which is calling WCF service) you can load the xml string in XmlDocument using LoadXml method. Like below
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
这篇关于使用WCF以XML返回DataTable数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文