12

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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
JJ.
  • 9,580
  • 37
  • 116
  • 189

6 Answers6

15

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

Jan
  • 2,168
  • 2
  • 19
  • 28
  • 1
    that worked :) ... i can only accept your answer in 10 mins tho. thx Jan! – JJ. Aug 10 '12 at 14:19
  • how would I add a value for "location" though? it's just between the <> > ... ?? – JJ. Aug 10 '12 at 14:21
  • Anytime :) Look at the `InnerText` property of XmlNode. – Jan Aug 10 '12 at 14:23
  • 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 look – JJ. Aug 10 '12 at 14:27
  • 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. – Jan Aug 10 '12 at 14:31
  • im still unable to set a value between VALUE . I thought node.Attributed["location"].InnerText = "location" would work.. no luck tho :( – JJ. Aug 10 '12 at 14:49
  • 1
    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 here – Jan Aug 10 '12 at 20:32
6
xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Description").InnerText = "My Description";
xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Location").InnerText = "My Location";
Igor Popov
  • 9,795
  • 7
  • 55
  • 68
Ramzay
  • 181
  • 4
  • 12
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);
JJ.
  • 9,580
  • 37
  • 116
  • 189
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);
Nickon
  • 9,652
  • 12
  • 64
  • 119
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

Robert Zahm
  • 1,877
  • 2
  • 12
  • 8
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