通过一个XML文件迭代选定节点 [英] Iterate through an xml file for a selected node

查看:258
本文介绍了通过一个XML文件迭代选定节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为image.xml xml文件

i have an xml file called image.xml

 <?xml version="1.0"?>

 <Image>
     <Overview>0</Overview>
     <Gallery1>0</Gallery1>
     <Gallery2>0</Gallery2>
 </Image>

code

string strGallery  =textbox1.text;
  lets  say text box  contains value  = Gallery1

当使用文件上传控制在gallery1添加图片
当我将图像保存在相应的文件夹,如:C:\\演示\\图像。

when an add an image in the gallery1 using an file upload control when i save the image in the respective folder like: c:\demo\image..

现在我应该看了上面的xml文件,因为我已经在gallery1文件夹中添加图像,现在我应该用1即增加值,因为我已经添加了图像的gallery1文件夹。

now i should read the above xml file as i have added an image in the gallery1 folder now i should increment that value by "1" because i have added an image to the gallery1 folder.

0 intaiilly这是
 添加图像后,现在我应该把它变成1
像这样的,如果我在库2文件夹中添加图片

0 intaiilly which was after adding the image now i should it become 1 like this if i have added an image in the gallery2 folder

然后我应该加&LT; 库2→1&LT; /库2方式&gt; 所以,如果,如​​果我在库2添加更多图片下的时间,然后计数应该是2

then i should incremented <Gallery2>1</Gallery2>. so if next time if i add one more image in gallery2 then the count should be 2

让我怎么能遍历所需库的元素
 如果监守在我的文本框为gallery1然后gallery1数类型应该incremetd
 如果在我的文本框为库2然后库2计数类型应incremetd
  然后保存XML文件一旦modifcation完成。

so how can i loop through the elements for the desired gallery becuase if type in my textbox as gallery1 then gallery1 count should be incremetd if type in my textbox as gallery2 then gallery2 count should be incremetd and then save the xml file once modifcation is done .

凭什么我achive此功能
 谢谢

so how can i achive this functionality thank you

那么,如何可以achive这个

so how can i achive this one

推荐答案

我不知道如果我理解正确的话,但尝试这样的:

I'm not sure if I understood it correctly, but try something like this:

XmlDocument xml = new XmlDocument();
xml.LoadXml("<Image>..."); // or xml.Load("yourfile.xml");

string name = "Gallery1";
XmlElement gallery = xml.SelectSingleNode("//" + name) as XmlElement;
if(gallery == null)
{
    gallery = xml.CreateElement(name);
    gallery.InnerText = "1";
    xml.DocumentElement.AppendChild(gallery);
}
 else
{
   gallery.InnerText = (Int32.Parse(gallery.InnerText) + 1).ToString();
}

这篇关于通过一个XML文件迭代选定节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆