小弟初学ASP.NET 请教大家生成如下XML改怎么写代码,谢谢
易做图作数据库得到一个DATATABLE 然后想把数据生成一个固定的XML XML格式如下<?xml version="1.0" encoding="utf-8"?>
<root>
<ShopInfo>
<DataRow>
<Shop_ID>111</Shop_ID>
<User_ID>222</User_ID>
<USer_Name>....</User_Name>
.....
.....
.....
</DataRow>
<DataRow>
<Shop_ID>222</Shop_ID>
<User_ID>333</User_ID>
<USer_Name>....</User_Name>
.....
.....
.....
</DataRow>
<DataRow>
<Shop_ID>444</Shop_ID>
<User_ID>555</User_ID>
<USer_Name>....</User_Name>
.....
.....
.....
</DataRow>
</ShopInfo>
</root>
可是我生成的代码格式如下
<?xml version="1.0" encoding="utf-8"?>
<root>
<ShopInfo>
<DataRow ShopID="3" User_ID="77">
</DataRow>
</ShopInfo>
</root>
具体代码如下
public void AchieveShopInfo(int shopID)
{
Model.ST_ShopInfo organ = new Model.ST_ShopInfo();
DataTable tempShopInfo = organ.BLL_AchieveLoadInfo(shopID);
XmlTextWriter writer = new XmlTextWriter(System.Web.HttpContext.Current.Server.MapPath("../images/Menu_1.xml"), System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("root");
for(int i=0;i<tempShopInfo.DataSet.Tables[0].Rows.Count;i++)
{
writer.WriteStartElement("ShopInfo");//添加子集
writer.WriteStartElement("DataRow");
//XmlElement Node1 = xmlDoc.CreateElement("Shop_ID");
//Node1.InnerText = tempShopInfo.DataSet.Tables[0].Rows[i]["Shop_ID"].ToString().Trim();
//XmlElement Node2 = xmlDoc.CreateElement("User_ID");
//Node2.InnerText = tempShopInfo.DataSet.Tables[0].Rows[i]["User_ID"].ToString().Trim();
writer.WriteAttributeString("ShopID", tempShopInfo.DataSet.Tables[0].Rows[i]["Shop_ID"].ToString().Trim());
writer.WriteAttributeString("User_ID", tempShopInfo.DataSet.Tables[0].Rows[i]["User_ID"].ToString().Trim());
}
writer.WriteFullEndElement();//关闭根集
writer.Close();
}
请问改如何修改 --------------------编程问答--------------------
private string CDataToXml(DataTable Dt)--------------------编程问答-------------------- 你这是STRING类型的把 我要返回XML格式的啊 --------------------编程问答--------------------
{
if (Dt != null)
{
MemoryStream ms = null;
XmlTextWriter xmlwrite = null;
try
{
ms = new MemoryStream();
xmlwrite = new XmlTextWriter(ms, Encoding.Unicode);
Dt.WriteXml(xmlwrite);
int count = (int)ms.Length;
byte[] temp = new byte[count];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(temp, 0, count);
UnicodeEncoding ucode = new UnicodeEncoding();
string returnValue = ucode.GetString(temp).Trim();
return returnValue;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (Dt == null)
{
xmlwrite.Close();
ms.Close();
ms.Dispose();
}
}
}
else
{
return "";
}
}
把string 写入xml的文本就得啦。
补充:.NET技术 , ASP.NET