How to change XML node values
Asked Answered
A

6

13

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?

Annorah answered 10/8, 2012 at 14:15 Comment(0)
A
4

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);
Annorah answered 10/8, 2012 at 14:32 Comment(0)
E
15

With XPath. XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet"); selects your root node.

Entablature answered 10/8, 2012 at 14:17 Comment(7)
that worked :) ... i can only accept your answer in 10 mins tho. thx Jan!Annorah
how would I add a value for "location" though? it's just between the <> </> ... ??Annorah
Anytime :) Look at the InnerText property of XmlNode.Entablature
by the way, username and description are working OK but im getting an error when i need to change the attachment NAME, i edited my post with my code, please take a lookAnnorah
Yes. node.Attributes["name"] selects an existing attribute. PolicyChangeSet does not contain a name attribute. Try node = node.SelectSingleNode("Attachment"); before you change the name-attribute. That selects the Attachment-node.Entablature
im still unable to set a value between <location> VALUE </location>. I thought node.Attributed["location"].InnerText = "location" would work.. no luck tho :(Annorah
Location is a node, not an attribute. Also XPath is case sensitive. node = xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Location"); and then node.InnerText = "myLocation" is the way to go hereEntablature
I
6
xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Description").InnerText = "My Description";
xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Location").InnerText = "My Location";
Iveyivie answered 12/11, 2014 at 1:57 Comment(0)
A
4

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);
Annorah answered 10/8, 2012 at 14:32 Comment(0)
U
4

Use LINQ To XML:)

XDocument doc = XDocument.Load(path);
IEnumerable<XElement> policyChangeSetCollection = doc.Elements("PolicyChangeSet");

foreach(XElement node in policyChangeSetCollection)
{
   node.Attribute("username").SetValue(someVal1);
   node.Attribute("description").SetValue(someVal2);
   XElement attachment = node.Element("attachment");
   attachment.Attribute("name").SetValue(someVal3);
   attachment.Attribute("contentType").SetValue(someVal4);
}

doc.Save(path);
Uncrowned answered 10/8, 2012 at 14:36 Comment(0)
E
1

In your SelectSingleNode method, you need to provide an XPath expression the find the node that you are looking to select. If you Google XPath you will find many resources for this.

http://www.csharp-examples.net/xml-nodes-by-name/

If you need to add these to each node, you can start at the top and iterate over all of the children.

http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.aspx

Eggert answered 10/8, 2012 at 14:21 Comment(0)
C
0
For setting value to XmlNode: 
 XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet");
            node["username"].InnerText = AppVars.Username;
            node["description"].InnerText = "Adding new .tiff image.";
            node["name"].InnerText = "POLICY";
            node["contentType"].InnerText = "content Typeeee";

For Getting value to XmlNode: 
username=node["username"].InnerText 
Cutting answered 13/8, 2015 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.