-1

I have a xml file and wanted to update and save the value of target load with C# code. My code is as below which is trying to xml shown below -

var fileName = textBox1.Text;
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            xmlDoc.Load(fileName);
            xmlDoc.SelectSingleNode("factoryTest/targetLoad").InnerText = "80";
            xmlDoc.Save(fileName);




<?xml version="1.0" encoding="utf-8"?>
<factoryTest xmlns="urn:gcpm">
  <targetLoad>90</targetLoad>
  <isAccepted>true</isAccepted>
  <isCertified>true</isCertified>
  <isAtRatingConditions>true</isAtRatingConditions>
  <supervisorName>Eric Larson</supervisorName>
</factoryTest>
5
  • 5
    Possible duplicate of edit Xml File using C# Commented Dec 15, 2018 at 22:47
  • @Somnath Please go thru the question mentioned in the comment above and try to solve your issue using that approach. If you face any issue in that, you can post it here. Commented Dec 15, 2018 at 22:52
  • I have already tried several options but none was helping my scenario. var fileName = textBox1.Text; System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.Load(fileName); xmlDoc.SelectSingleNode("factoryTest/targetLoad").InnerText = "80"; xmlDoc.Save(fileName); Commented Dec 15, 2018 at 23:07
  • @Somnath you can edit your question with that code and provide details about what is not working. Commented Dec 15, 2018 at 23:09
  • I have a xml file whose content needs to be updated and saved with the help of C# code. Commented Dec 15, 2018 at 23:19

1 Answer 1

0

I have resolved this issue.

XDocument xdoc = XDocument.Load(tFileName);
            xdoc.Elements("{urn:gcpm}factoryTest").Elements("{urn:gcpm}targetLoad").FirstOrDefault().Value = textBox2.Text;
            xdoc.Save(tFileName);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.