请教高手如何用c#修改XML文件的标记里的内容
我想用程序修改如下xml文件的内容:<?xmlversion="1.0"encoding="utf-8"?><configuration><connectionStrin...
我想用程序修改如下xml文件的内容:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="2008" connectionString="text"/>
</connectionStrings>
</configuration>
我想修改的内容是:connectionSring里的"text"内容,请教高手如何操作? 展开
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="2008" connectionString="text"/>
</connectionStrings>
</configuration>
我想修改的内容是:connectionSring里的"text"内容,请教高手如何操作? 展开
2个回答
展开全部
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(Server.MapPath("***.xml"));
XmlNodeList list = XMLDoc.GetElementsByTagName("add");
for (int i = 0; i < list.Count; i++)
{
if (list[i].Attributes[0].Value == "2008")
{
list[i].Attributes[1].Value = "text";//在这里改
}
}
StreamWriter swriter=new StreamWriter(Server.MapPath("******.xml"));
XmlTextWriter xw = new XmlTextWriter(swriter);
xw.Formatting = Formatting.Indented;
XMLDoc.WriteTo(xw);
xw.Close();
swriter.Close();
XMLDoc.Load(Server.MapPath("***.xml"));
XmlNodeList list = XMLDoc.GetElementsByTagName("add");
for (int i = 0; i < list.Count; i++)
{
if (list[i].Attributes[0].Value == "2008")
{
list[i].Attributes[1].Value = "text";//在这里改
}
}
StreamWriter swriter=new StreamWriter(Server.MapPath("******.xml"));
XmlTextWriter xw = new XmlTextWriter(swriter);
xw.Formatting = Formatting.Indented;
XMLDoc.WriteTo(xw);
xw.Close();
swriter.Close();
展开全部
string strxml = @"<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="2008" connectionString="text"/>
</connectionStrings>
</configuration>";
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Loadxml(strxml );
XMLDoc.selectsinglenode("connectionSring").InerText = "text1";
<configuration>
<connectionStrings>
<add name="2008" connectionString="text"/>
</connectionStrings>
</configuration>";
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Loadxml(strxml );
XMLDoc.selectsinglenode("connectionSring").InerText = "text1";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询