Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have got problems to update a XML Document. I am not getting the Selected attribute value.And know I don't know how to update the childNode id value with XmlDocument & XmlNode.

Is there some property to update for the selectedAttibute value? Please some one please help me on this. I have tried the below code.

What I have tried:

This is Languages.xml

<?xml version="1.0" encoding="utf-8" ?>
<LANGUAGES>
	<LANGUAGE>
		<Name>English</Name>
		<CULTURE>en-US</CULTURE>
    <Selected>Y</Selected>
	</LANGUAGE>
	<LANGUAGE>
		<Name>German</Name>
		<CULTURE>de-DE</CULTURE>
    <Selected>Y</Selected>
	</LANGUAGE>
	<LANGUAGE>
		<Name>Chines</Name>
		<CULTURE>zh-CN</CULTURE>
    <Selected>Y</Selected>
	</LANGUAGE>
</LANGUAGES>


And also I have tried like this but I am getting selectedAttribute value as null.

private void OnSelectedLanguageChanged(Model.Language selectedLanguage)
       {

           XmlDocument doc = new XmlDocument();
           doc.Load("Languages.xml");

           XmlNodeList aNodes = doc.SelectNodes("/LANGUAGES/LANGUAGE");
           // loop through all nodes
           foreach (XmlNode aNode in aNodes)
           {

               XmlAttribute selectedAttribute = aNode.Attributes["Selected"];

               // check if that attribute even exists...
               if (selectedAttribute != null)
               {
                   // if yes - read its current value
                   string currentValue = selectedAttribute.Value;


                   if (!string.IsNullOrEmpty(currentValue))
                   {
                       selectedAttribute.Value = "N";
                   }
               }
           }
           doc.Save(@"Languages.xml");
       }
Posted
Comments
[no name] 13-Jun-21 0:49am    
"Selected" is an Element, not an Attribute. The node s/b of type Element and you s/b able to access its .Value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900