日期:2014-05-20  浏览次数:20440 次

asp.net如何从数据库中读取数据生成自定义的XML文件!高分请教?
我要实现的是每次第一次加载页面的时候从数据库中读取数据生成自定义的XML文件,而且希望实现的是每次首次加载页面时xml会更新新的元素内容,而不是继续增加,而是覆盖以前的内容,唉~!我表达能力不行,刚学asp.net,很多都不熟悉,望大家给个成熟点的答案给我看看,我给分.

------解决方案--------------------
用.Net的XmlDocument很好操作的啊。
参考代码:
private void CreateMenuXmlData()
{
string _XmlString;
XmlDocument _XmlDoc = new XmlDocument();
_ConnString = gc.GetConnString();

_SqlQuery = "SELECT [PurviewCode], [Title], [Url], [ParentCode] FROM [tb_menu_info] WHERE [Show] = 0 ORDER BY [PurviewCode] ";
DataTable _MenuTable = SqlHelper.ExecuteDataset(_ConnString, CommandType.Text, _SqlQuery).Tables[0];
DataRow _MenuRow;
XmlElement _MenuNode = _XmlDoc.DocumentElement;
XmlElement _ChildNode = _XmlDoc.DocumentElement;

_XmlString = " <?xml version=\ "1.0\ " encoding=\ "utf-8\ " ?> ";
_XmlString += " <root Title=\ "根目录\ " Url=\ "javascript:void(0)\ "> </root> ";
_XmlDoc.LoadXml(_XmlString);
XmlNode root = _XmlDoc.DocumentElement;

for (int i = 0; i < _MenuTable.Rows.Count; i++)
{
_MenuRow = _MenuTable.Rows[i];
if (_MenuRow[ "ParentCode "].ToString() == "0 ")
{
_MenuNode = _XmlDoc.CreateElement( "MenuNode ");
_MenuNode.SetAttribute( "Title ", _MenuRow[ "Title "].ToString());
_MenuNode.SetAttribute( "Url ", "javascript:void(0) ");
root.AppendChild(_MenuNode);
}
else
{
_ChildNode = _XmlDoc.CreateElement( "ChildNode ");
_ChildNode.SetAttribute( "Title ", _MenuRow[ "Title "].ToString());
_ChildNode.SetAttribute( "Url ", _MenuRow[ "Url "].ToString());
_MenuNode.AppendChild(_ChildNode);
}
}

_XmlDoc.Save(Request.PhysicalApplicationPath + "App_Data\\XmlMenu.xml ");
}