[c#] How to modify existing XML file with XmlDocument and XmlNode in C#

I already implemented to create the XML file below with XmlTextWriter when application initialization.

And know I don't know how to update the childNode id value with XmlDocument & XmlNode.

Is there some property to update the id value? I tried InnerText but failed. thank you.

<?xml version="1.0" encoding="UTF-8"?>
<Equipment xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <License licenseId="" licensePath=""/>
  <DataCollections>
    <GroupAIDs>
      <AID id="100">
        <Variable id="200"/>
        <Variable id="201"/>
      </AID>
      <AID id="">
        <Variable id="205"/>
      </AID>
      <AID id="102"/>
    </GroupAIDs>
    <GroupBIDs>
      <BID id="2000">
        <AID id="100"/>
      </BID>
      <BID id="2001">
        <AID id="101"/>
        <AID id="102"/>
      </BID>
    </GroupBIDs>
    <GroupCIDs>
      <BID id="8"/>
      <BID id="9"/>
      <BID id="10"/>
    </GroupCIDs>
  </DataCollections>
</Equipment>

This question is related to c# xml xmldocument xmlnode

The answer is


You need to do something like this:

// instantiate XmlDocument and load XML from file
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\test.xml");

// get a list of nodes - in this case, I'm selecting all <AID> nodes under
// the <GroupAIDs> node - change to suit your needs
XmlNodeList aNodes = doc.SelectNodes("/Equipment/DataCollections/GroupAIDs/AID");

// loop through all AID nodes
foreach (XmlNode aNode in aNodes)
{
   // grab the "id" attribute
   XmlAttribute idAttribute = aNode.Attributes["id"];

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

      // here, you can now decide what to do - for demo purposes,
      // I just set the ID value to a fixed value if it was empty before
      if (string.IsNullOrEmpty(currentValue))
      {
         idAttribute.Value = "515";
      }
   }
}

// save the XmlDocument back to disk
doc.Save(@"D:\test2.xml");

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to xml

strange error in my Animation Drawable How do I POST XML data to a webservice with Postman? PHP XML Extension: Not installed How to add a Hint in spinner in XML Generating Request/Response XML from a WSDL Manifest Merger failed with multiple errors in Android Studio How to set menu to Toolbar in Android How to add colored border on cardview? Android: ScrollView vs NestedScrollView WARNING: Exception encountered during context initialization - cancelling refresh attempt

Examples related to xmldocument

Why "Data at the root level is invalid. Line 1, position 1." for XML Document? Read XML file into XmlDocument Getting specified Node values from XML document XML Document to String How to modify existing XML file with XmlDocument and XmlNode in C# Convert XmlDocument to String XDocument or XmlDocument Read XML Attribute using XmlDocument How to change XML Attribute What is the simplest way to get indented XML with line breaks from XmlDocument?

Examples related to xmlnode

how to get the attribute value of an xml node using java How to modify existing XML file with XmlDocument and XmlNode in C# Modify XML existing content in C# What's the difference between an element and a node in XML?