locked
How to update node value in XML? RRS feed

  • Question

  • User417741264 posted

    I have a string like "<NavigationDetails Title=\"Nw\" Description=\"Nw\" NavigateTo=\"Nw\" ImageUrl=\"\" MenuOrder=\"\" />"

    i want to update MenuOrder value to 1 if it's value is null or empty.How to do this?

    Thursday, April 16, 2015 4:42 AM

Answers

  • User516094431 posted

    maheshvishnu

    "<NavigationDetails Title=\"Nw\" Description=\"Nw\" NavigateTo=\"Nw\" ImageUrl=\"\" MenuOrder=\"\" />"

    Parse this string in XmlDocument.

    XmlDocument xml = new XmlDocument();
    
    xml.LoadXml("<NavigationDetails Title=\"Nw\" Description=\"Nw\" NavigateTo=\"Nw\" ImageUrl=\"\" MenuOrder=\"\" />");

    Then add value:

    IEnumerator ie = doc.SelectNodes("/NavigationDetails").GetEnumerator();
    
                while (ie.MoveNext())
                {
                    if ((ie.Current as XmlNode).Attributes["key"].Value == "MenuOrder")
                    {
                        (ie.Current as XmlNode).Attributes["value"].Value = "hello";
                    }
                }
    
                xml.Save(path);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 16, 2015 5:09 AM
  • User61956409 posted

    Hi maheshvishnu,

    Thanks for your post.

    Please refer to the following sample to update the value of XML attribute.

    XDocument xdoc = XDocument.Load(Server.MapPath("NavigationDetails.xml"));
    
    foreach (var element in xdoc.Descendants("NavigationDetails"))
    {
        string MenuOrder = element.Attribute("MenuOrder").Value.ToString();
    
        if (MenuOrder=="")
        {
            element.Attribute("MenuOrder").Value = "1";
        }
    }
    
    xdoc.Save(Server.MapPath("NavigationDetails.xml")); 
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 17, 2015 1:27 AM

All replies

  • User516094431 posted

    maheshvishnu

    "<NavigationDetails Title=\"Nw\" Description=\"Nw\" NavigateTo=\"Nw\" ImageUrl=\"\" MenuOrder=\"\" />"

    Parse this string in XmlDocument.

    XmlDocument xml = new XmlDocument();
    
    xml.LoadXml("<NavigationDetails Title=\"Nw\" Description=\"Nw\" NavigateTo=\"Nw\" ImageUrl=\"\" MenuOrder=\"\" />");

    Then add value:

    IEnumerator ie = doc.SelectNodes("/NavigationDetails").GetEnumerator();
    
                while (ie.MoveNext())
                {
                    if ((ie.Current as XmlNode).Attributes["key"].Value == "MenuOrder")
                    {
                        (ie.Current as XmlNode).Attributes["value"].Value = "hello";
                    }
                }
    
                xml.Save(path);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 16, 2015 5:09 AM
  • User61956409 posted

    Hi maheshvishnu,

    Thanks for your post.

    Please refer to the following sample to update the value of XML attribute.

    XDocument xdoc = XDocument.Load(Server.MapPath("NavigationDetails.xml"));
    
    foreach (var element in xdoc.Descendants("NavigationDetails"))
    {
        string MenuOrder = element.Attribute("MenuOrder").Value.ToString();
    
        if (MenuOrder=="")
        {
            element.Attribute("MenuOrder").Value = "1";
        }
    }
    
    xdoc.Save(Server.MapPath("NavigationDetails.xml")); 
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 17, 2015 1:27 AM