asp.net怎么读取或保存xml文件里的数据
asp.net怎么读取或保存、修改xml文件里的数据信息。能不能给我详细的代码(例子)和xml文件...
asp.net怎么读取或保存、修改xml文件里的数据信息。
能不能给我详细的代码(例子)和xml文件 展开
能不能给我详细的代码(例子)和xml文件 展开
1个回答
展开全部
<root>
<env>
<param name="rate" value="3" />
<param name="url" value="3" />
</env>
<vr startpano="start" width="10" height="540" x="0" y="0" >
<pano name="url" url="Car/Model/InteriorPanorama/BJXD_i30/S-max.jpg">
</pano>
</vr>
<background file="control/bg.jpg">
</background>
</root>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class SomePages_OperateXML : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = ReadXML();
lblReadXML.Text = str;
SaveXML();
}
private string ReadXML()
{
string strReturn = string.Empty;
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/images/config.xml"));
XmlElement root = doc.DocumentElement;
//定义要求匹配项 name="url"
string path = @"//*[@name='url']";
//在当前节点根据匹配项进行寻找
foreach (XmlNode node in root.SelectNodes(path))
{
strReturn += "匹配项:" + path;
strReturn += "节点:" + node.Name;
strReturn += "<br/>";
}
//直接寻找指定节点读取值
XmlElement x = (XmlElement)root.SelectSingleNode("vr");
if (x != null)
{
strReturn += x.GetAttribute("startpano");
}
return strReturn;
}
private void SaveXML()
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/images/config.xml"));
XmlElement root = doc.DocumentElement;
XmlElement x = (XmlElement)root.SelectSingleNode("vr");
if (x != null)
{
//设置节点属性
x.SetAttribute("width1", "10");
//创建节点
XmlElement x1 = doc.CreateElement("test");
x1.SetAttribute("width", "100");
//添加节点
x.AppendChild(x1);
//保存xml
doc.Save(Server.MapPath("~/images/config.xml"));
}
}
}
<env>
<param name="rate" value="3" />
<param name="url" value="3" />
</env>
<vr startpano="start" width="10" height="540" x="0" y="0" >
<pano name="url" url="Car/Model/InteriorPanorama/BJXD_i30/S-max.jpg">
</pano>
</vr>
<background file="control/bg.jpg">
</background>
</root>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class SomePages_OperateXML : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = ReadXML();
lblReadXML.Text = str;
SaveXML();
}
private string ReadXML()
{
string strReturn = string.Empty;
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/images/config.xml"));
XmlElement root = doc.DocumentElement;
//定义要求匹配项 name="url"
string path = @"//*[@name='url']";
//在当前节点根据匹配项进行寻找
foreach (XmlNode node in root.SelectNodes(path))
{
strReturn += "匹配项:" + path;
strReturn += "节点:" + node.Name;
strReturn += "<br/>";
}
//直接寻找指定节点读取值
XmlElement x = (XmlElement)root.SelectSingleNode("vr");
if (x != null)
{
strReturn += x.GetAttribute("startpano");
}
return strReturn;
}
private void SaveXML()
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/images/config.xml"));
XmlElement root = doc.DocumentElement;
XmlElement x = (XmlElement)root.SelectSingleNode("vr");
if (x != null)
{
//设置节点属性
x.SetAttribute("width1", "10");
//创建节点
XmlElement x1 = doc.CreateElement("test");
x1.SetAttribute("width", "100");
//添加节点
x.AppendChild(x1);
//保存xml
doc.Save(Server.MapPath("~/images/config.xml"));
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询