Search code examples
c#xmlxmlnode

How to change XML node values


I have an XML (this is exactly what it looks like):

<PolicyChangeSet schemaVersion="2.1" username="" description="">
    <Attachment name="" contentType="">
        <Description/>
        <Location></Location>
    </Attachment>
</PolicyChangeSet>

This is on the user's machine.

I need to add values to each node: username, description, attachment name, contenttype, and location.

This is what I have so far:

string newValue = string.Empty;
XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(filePath);
XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet");
node.Attributes["username"].Value = AppVars.Username;
node.Attributes["description"].Value = "Adding new .tiff image.";
node.Attributes["name"].Value = "POLICY";
node.Attributes["contentType"].Value = "content Typeeee";

//node.Attributes["location"].InnerText = "zzz";

xmlDoc.Save(filePath);

Any help?


Solution

  • Got it with this -

    xmlDoc.Load(filePath);
                XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet");
                node.Attributes["username"].Value = AppVars.Username;
                node.Attributes["description"].Value = "Adding new .tiff image.";
    
                node = xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment");
                node.Attributes["name"].Value = "POLICY";
                node.Attributes["contentType"].Value = "content Typeeee";
    xmlDoc.Save(filePath);