用DataSet生成指定格式的XML
最近用FLASH调用DataSet生成的XML发现DataSet的XML都有固定格式,网上也没有太好的方法,于是自己写了下给大家参考
protected void Page_Load(object sender, EventArgs e)
{
TBLL.TSlide slidebll = new TBLL.TSlide();
DataSet ds = slidebll.GetAllList();
XmlDataDocument xdd = SetItemsCountAttribute(ds);
Response.Clear();
xdd.Save(Response.OutputStream);
Response.End();
}
private XmlDataDocument SetItemsCountAttribute(DataSet ds)
{
try
{
XmlDataDocument xmlDoc;
int ItemCount = 0;
ds.DataSetName = strRootNodeName;
ds.EnforceConstraints = false;
xmlDoc = new XmlDataDocument();
XmlNode xmlDocNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmlDoc.AppendChild(xmlDocNode);
XmlNode viewer = xmlDoc.CreateElement("viewer");
XmlAttribute interval = xmlDoc.CreateAttribute("interval");
interval.Value = "4000";
viewer.Attributes.Append(interval);
XmlAttribute isRandom = xmlDoc.CreateAttribute("isRandom");
isRandom.Value = "1";
viewer.Attributes.Append(isRandom);
xmlDoc.AppendChild(viewer);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
XmlNode item = xmlDoc.CreateElement("item");
XmlAttribute title = xmlDoc.CreateAttribute("title");
title.Value = ds.Tables[0].Rows[i]["Title"].ToString();
item.Attributes.Append(title);
XmlAttribute img = xmlDoc.CreateAttribute("img");
img.Value = ds.Tables[0].Rows[i]["ImgUrl"].ToString();
item.Attributes.Append(img);
XmlAttribute url = xmlDoc.CreateAttribute("url");
url.Value = ds.Tables[0].Rows[i]["LinkUrl"].ToString();
item.Attributes.Append(url);
XmlAttribute target = xmlDoc.CreateAttribute("target");
target.Value = "_blank";
item.Attributes.Append(target);
viewer.AppendChild(item);
}
return xmlDoc;
}
catch (Exception e)
{
string strMsg = e.Message;
return null;
}
}
{
TBLL.TSlide slidebll = new TBLL.TSlide();
DataSet ds = slidebll.GetAllList();
XmlDataDocument xdd = SetItemsCountAttribute(ds);
Response.Clear();
xdd.Save(Response.OutputStream);
Response.End();
}
private XmlDataDocument SetItemsCountAttribute(DataSet ds)
{
try
{
XmlDataDocument xmlDoc;
int ItemCount = 0;
ds.DataSetName = strRootNodeName;
ds.EnforceConstraints = false;
xmlDoc = new XmlDataDocument();
XmlNode xmlDocNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmlDoc.AppendChild(xmlDocNode);
XmlNode viewer = xmlDoc.CreateElement("viewer");
XmlAttribute interval = xmlDoc.CreateAttribute("interval");
interval.Value = "4000";
viewer.Attributes.Append(interval);
XmlAttribute isRandom = xmlDoc.CreateAttribute("isRandom");
isRandom.Value = "1";
viewer.Attributes.Append(isRandom);
xmlDoc.AppendChild(viewer);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
XmlNode item = xmlDoc.CreateElement("item");
XmlAttribute title = xmlDoc.CreateAttribute("title");
title.Value = ds.Tables[0].Rows[i]["Title"].ToString();
item.Attributes.Append(title);
XmlAttribute img = xmlDoc.CreateAttribute("img");
img.Value = ds.Tables[0].Rows[i]["ImgUrl"].ToString();
item.Attributes.Append(img);
XmlAttribute url = xmlDoc.CreateAttribute("url");
url.Value = ds.Tables[0].Rows[i]["LinkUrl"].ToString();
item.Attributes.Append(url);
XmlAttribute target = xmlDoc.CreateAttribute("target");
target.Value = "_blank";
item.Attributes.Append(target);
viewer.AppendChild(item);
}
return xmlDoc;
}
catch (Exception e)
{
string strMsg = e.Message;
return null;
}
}