在特定位置写入XML文件 [英] Write in a XML File at a particular Lacation
本文介绍了在特定位置写入XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个类似的xml文件.
Hello All,
I have a xml file like.
<?xml version="1.0" encoding="utf-8"?>
<config>
<galleries>
<folder name="Nature">
<gallery name="Landscapes">
<img src="nature01.jpg" width="351" height="550" caption="Some photo" />
<img src="nature02.jpg" width="559" height="420" caption="Some photo" />
</gallery>
<gallery name="Water">
<img src="nature01.jpg" width="351" height="550" caption="Some photo" />
<img src="nature02.jpg" width="559" height="420" caption="Some photo" />
</gallery>
<gallery name="Mars">
<img src="mars01.jpg" width="431" height="565" caption="Some photo" />
</gallery>
</folder>
<folder name="Constructs">
<gallery name="Buildings">
<img src="construct01.jpg" width="328" height="500" caption="Some photo 1" />
<img src="construct02.jpg" width="323" height="500" caption="Some photo 2" />
</gallery>
<gallery name="Installations">
<img src="construct01.jpg" width="328" height="500" caption="Some photo" />
<img src="construct02.jpg" width="323" height="500" caption="Some photo" />
</gallery>
</folder>
<gallery name="Venice">
<img src="venice01.jpg" width="448" height="418" caption="Some photo" />
</gallery>
</galleries>
<section name="About us" width="300" height="420" bydefault="true">
<title>Full dynamic website demo</title>
<p>Demo Demo</p>
</section>
<titleandslogan>
<p class="ptitleandslogan"><span class="spantitle">Delight</span><br />photography studio</p>
</titleandslogan>
</config>
我必须动态添加&更新图库中的图像,并且该部分也是我的新手,我该怎么做呢.
i have to dynamically Add & Update the images inside the gallery and the section also i am new in development how can i do it plz help.
推荐答案
以下是执行相同操作的一些选项
1.使用XmlDocument
Following are some of the options to do the same
1. Using XmlDocument
XmlDocument doc = new XmlDocument();
doc.Load("D:\\build.xml");
XmlNode root = doc.DocumentElement;
XmlNode myNode = root.SelectSingleNode("galleries::folder");
myNode.Value = "blabla";
doc.Save("D:\\build.xml");
2.您也可以使用LINQ进行相同的操作
2. You can also use LINQ to do the same
using System.Xml.Linq;
XDocument xmlFile = XDocument.Load("D:\\build.xml");
var query = from c in xmlFile.Elements("galleries").Elements("folder")
select c; foreach (XElement folder in query)
{
folder.Attribute("attr1").Value = "MyNewValue";
}
xmlFile.Save("build.xml");
这篇关于在特定位置写入XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文