C#中xml添加数据?
xml文件:<?xmlversion="1.0"encoding="utf-8"?><playlistversion="1"><trackList><track><tit...
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<playlist version="1">
<trackList>
<track>
<title>标题1</title>
<location>indexSwf/images/01.jpg</location>
</track>
<track>
<title>标题2</title>
<location>indexSwf/images/02.jpg</location>
</track>
</trackList>
</playlist>
问题:现在向xml文件中插入“标题3”,请帮帮忙?非常感谢 展开
<?xml version="1.0" encoding="utf-8"?>
<playlist version="1">
<trackList>
<track>
<title>标题1</title>
<location>indexSwf/images/01.jpg</location>
</track>
<track>
<title>标题2</title>
<location>indexSwf/images/02.jpg</location>
</track>
</trackList>
</playlist>
问题:现在向xml文件中插入“标题3”,请帮帮忙?非常感谢 展开
展开全部
简单..把结构搞清楚了就简单了..
XmlNode xNode=xdoc.SelectSingleNode("playlist/trackList");
XmlElement xmlElem = xdoc.CreateElement("track");//track
XmlElement xmlElem1 = xdoc.CreateElement("title");//title
xmlElem1.InnerText = "标题3";
XmlElement xmlElem2 = xdoc.CreateElement("location");//location
xmlElem1.InnerText = "图片名称";
xmlElem.AppendChild(xmlElem1);
xmlElem.AppendChild(xmlElem2);
xNode.AppendChild(xmlElem);
xdoc.Save(configPath);
XmlNode xNode=xdoc.SelectSingleNode("playlist/trackList");
XmlElement xmlElem = xdoc.CreateElement("track");//track
XmlElement xmlElem1 = xdoc.CreateElement("title");//title
xmlElem1.InnerText = "标题3";
XmlElement xmlElem2 = xdoc.CreateElement("location");//location
xmlElem1.InnerText = "图片名称";
xmlElem.AppendChild(xmlElem1);
xmlElem.AppendChild(xmlElem2);
xNode.AppendChild(xmlElem);
xdoc.Save(configPath);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询