tags:

views:

81

answers:

2

hi,

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

hi this is my xml file i have an dropdown value with values [gallery1, gallery2, gallery3 so on . if user select an gallery2 and types in the text box as = 5 and clicks ok button then i need to load my above xml file and check what is gallery value we need to update but first we need to get the value from xml her i am trying to update galery2 so before i need get gallery2 value =4
then add the new value with old value{5+4=9) and then save . so now gallery2 will contain value as 9

result

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

I think what you are looking is

 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"));
    }
Muhammad Akhtar
why is this line code we are using ("//myChild[@ChildID='ref-3']");// what is 'ref-3' doing here
prince23
what is mychild represent here should i replace this with Image
prince23
I have update, plz check now.
Muhammad Akhtar
A: 

A more general-purpose version of this method would take the entire XPath to the element you want to change, instead of just assuming that it's a child of a top-level Image element.

private void AddElementValue(XmlDocument doc, string tagName, int valueToAdd)
{
   XmlElement elm = doc.SelectSingleElement("/Image/" + tagName);
   Debug.Assert(elm != null, "Didn't find " + tagName);
   int currentValue;
   if (int.TryParse(elm.InnerText, out currentValue))
   {
      elm.InnerText = (currentValue + valueToAdd).ToString();
      return;
   }
   Debug.Fail(elm.InnerText + " can't be parsed as an integer.");       
}
Robert Rossney