locked
how to update and save XMLNode value? RRS feed

  • Question

  • hello I have this xml code

    <AriaGostarInformation>
      <MenuInformation>
        <MenuNames Name="1">
          home
        </MenuNames>
        <MenuNames Name="2">
          pages
        </MenuNames>
        <MenuNames Name="3">
          blog
        </MenuNames>
        <MenuNames Name="4">
          gallery
        </MenuNames>
        <MenuNames Name="5">
          about us
        </MenuNames>
        <MenuNames Name="6">
        contact us
        </MenuNames>
        <SubMenuNames parentName="1">
          fgfgfgfgs
        </SubMenuNames>
        <SubMenuNames parentName="3">
        </SubMenuNames>
      </MenuInformation>
    </AriaGostarInformation>

    and c# code:

    public XmlNodeList getXmlNodeList(string XmlNodeName)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(_connection);
        XmlNodeList elemList = doc.GetElementsByTagName(XmlNodeName);
        return elemList;
    }
    
    public void updateXmlNodeWithAttribute(string XmlNodeName, string XmlAttributeName, List<string> XmlNodeAttribute, List<string> XmlNodeValue)
    {
        XmlDocument xdoc = new XmlDocument();
        xdoc.Load(_connection);
        XmlNodeList xnode = getXmlNodeList(XmlNodeName);
    
        for (int i = 0; i < XmlNodeAttribute.Count - 1; i++)
        {
            foreach (XmlNode item in xnode)
            {
                if (item.Attributes[XmlAttributeName].Value == XmlNodeAttribute[i].ToString())
                {
                    item.InnerText = XmlNodeValue[i].ToString();
                }
            }
        }
    
        xdoc.Save(_connection);
    }

    The getXmlNodeList method return Specified xmlnodelist from XML file and _connection is string xml address

    This code does not throw an error, but DOES NOT update the XML node.

    Please help me where is error in my code?

    Tuesday, January 7, 2014 6:50 AM

Answers

  • public void updateXmlNodeWithAttribute(string XmlNodeName, string XmlAttributeName, List<string> XmlNodeAttribute, List<string> XmlNodeValue)
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(_connection);
                XmlNodeList xnode = getXmlNodeList(XmlNodeName);

                for (int i = 0; i < XmlNodeAttribute.Count - 1; i++)
                {
                    foreach (XmlNode item in xnode)
                    {
                        if (item.Attributes[XmlAttributeName].Value == XmlNodeAttribute[i].ToString())
                        {
                            string attrValue =XmlNodeAttribute[i].ToString();
                             XmlNode XNode = xdoc.SelectSingleNode("//" +XmlNodeName +"[@"+ XmlAttributeName + "='" + attrValue +"']");
                            XNode.InnerText = XmlNodeValue[i].ToString();
                            
                           
                        }
                    }
                }

               xdoc.Save(_connection);
            }

    Tuesday, January 7, 2014 10:10 AM
  • The reason is that in the previous code when you are using

    item.InnerText = XmlNodeValue[i].ToString();

     you are just iterating through the nodes in xml. When you modify using InnerText ,you are modifying the text of the xnode and not the actual node in xml document. To do that you will have to select the particular node using the xpath from the xml document and modify the InnerText

    Tuesday, January 7, 2014 12:15 PM

All replies

  • Please check whether this part is executed or not:

      if (item.Attributes[XmlAttributeName].Value == XmlNodeAttribute[i].ToString())


    ASP.NET Questions
    Other Discussions
    FreeRice Donate
    Issues to report
    Free Tech Books Search

    Tuesday, January 7, 2014 7:06 AM
  • yes  this part is executed
    Tuesday, January 7, 2014 7:32 AM
  • Well……Your problem is a little strange……It would be better if you can package your whole proj (just a demo with a Console App) by submitting it and let us download to analyze that (Please use your local drive path instead).

    Please use SkyDrive


    ASP.NET Questions
    Other Discussions
    FreeRice Donate
    Issues to report
    Free Tech Books Search

    Tuesday, January 7, 2014 8:10 AM
  • public void updateXmlNodeWithAttribute(string XmlNodeName, string XmlAttributeName, List<string> XmlNodeAttribute, List<string> XmlNodeValue)
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(_connection);
                XmlNodeList xnode = getXmlNodeList(XmlNodeName);

                for (int i = 0; i < XmlNodeAttribute.Count - 1; i++)
                {
                    foreach (XmlNode item in xnode)
                    {
                        if (item.Attributes[XmlAttributeName].Value == XmlNodeAttribute[i].ToString())
                        {
                            string attrValue =XmlNodeAttribute[i].ToString();
                             XmlNode XNode = xdoc.SelectSingleNode("//" +XmlNodeName +"[@"+ XmlAttributeName + "='" + attrValue +"']");
                            XNode.InnerText = XmlNodeValue[i].ToString();
                            
                           
                        }
                    }
                }

               xdoc.Save(_connection);
            }

    Tuesday, January 7, 2014 10:10 AM
  • verrrrrrrrrrrrrrrrrrrrrrryyyyyyyyyyyyyyyyyyy thank's

    May explain me about this code?
    What is the reason?

    Tuesday, January 7, 2014 11:33 AM
  • The reason is that in the previous code when you are using

    item.InnerText = XmlNodeValue[i].ToString();

     you are just iterating through the nodes in xml. When you modify using InnerText ,you are modifying the text of the xnode and not the actual node in xml document. To do that you will have to select the particular node using the xpath from the xml document and modify the InnerText

    Tuesday, January 7, 2014 12:15 PM