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?

6 Answers 6

15

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

Sign up to request clarification or add additional context in comments.

7 Comments

that worked :) ... i can only accept your answer in 10 mins tho. thx Jan!
how would I add a value for "location" though? it's just between the <> </> ... ??
Anytime :) Look at the InnerText property of XmlNode.
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
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
|
6
xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Description").InnerText = "My Description";
xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Location").InnerText = "My Location";

Comments

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);

Comments

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);

Comments

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

Comments

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 

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.