如何从xml中检索特定节点的值并再次更新xml [英] How to retrieve an value from xml for a particular node and again update the xml

查看:59
本文介绍了如何从xml中检索特定节点的值并再次更新xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?xml version="1.0"?>
     <Image>    
        <Overview>13</Overview>  
        <Gallery1>1</Gallery1>    
        <Gallery2>4</Gallery2>
        <Gallery3>6</Gallery3>    
        <Gallery4>1</Gallery4>
     </Image>

这是我的xml文件.我有一个包含值的下拉值(gallery1,gallery2,gallery3等).如果用户选择gallery2并在文本框中键入5并单击确定"按钮,然后我需要加载上述XML文件并检查我们需要更新的图库值.但是首先,我们需要从XML获取价值.

This is my xml file. I have a dropdown value with values (gallery1, gallery2, gallery3 and so on). If user selects gallery2 and types 5 in the text box and clicks the OK button, then I need to load my above XML file and check what gallery value we need to update. But first we need to get the value from XML.

在这里,我正在尝试更新gallery2,因此首先我需要获取gallery2值(4)然后将新值与旧值(5 + 4 = 9)相加,然后保存.因此,gallery2现在将包含值9.

Here I am trying to update gallery2 so first I need get the gallery2 value (4) then add the new value with old value (5+4=9) and then save. So now gallery2 will contain the value 9.

结果

 <?xml version="1.0"?>
     <Image>    
        <Overview>13</Overview>  
        <Gallery1>1</Gallery1>    
        **<Gallery2>9</Gallery2>**
        <Gallery3>6</Gallery3>    
        <Gallery4>1</Gallery4>
     </Image>

推荐答案

我认为您正在寻找的是

 protected void Button12_Click(object sender, EventArgs e)
    {
        lbl = GetLabel(275, 20);
        //Declare and load new XmlDocument
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(MapPath("XmlSample.xml"));
        //delete a mode
        XmlNode node;
        node = xmlDoc.SelectSingleNode("//Image");
        node.ParentNode.RemoveChild(node);
        //create a node and add it
        XmlElement newElement =
        xmlDoc.CreateElement("myNewElement");
        node = xmlDoc.SelectSingleNode("//Image");
        node.ParentNode.InsertAfter(newElement, node);
        xmlDoc.Save(MapPath("XmlSampleModified.xml"));
    }

这篇关于如何从xml中检索特定节点的值并再次更新xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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