Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the value of an attribute in an XML document?

Tags:

I have an XML document below and there is a tag called <FormData> in side this tag it as an attribute called FormId="d617a5e8-b49b-4640-9734-bc7a2bf05691"

I would like to change that value in C# code?

    XmlDocument xmlDoc = new XmlDocument();     xmlDoc.Load(MapPath(tempFolderPathAlt + "dvforms" + "\\XmlDataTemplate.xml"));     //Change value of FormID     xmlDoc.Save(tempFolderPath + "data.xml"); 

Be is my XML document:

<?xml version="1.0"?> <FormData Platform="Android" PlatformVersion="100" Version="966" DataVersion="1" Description="Investec - Res" FormId="d617a5e8-b49b-4640-9734-bc7a2bf05691" FileId="e6202ba2-3658-4d8e-836a-2eb4902d441d" EncryptionVerification="" CreatedBy="Bob" EditedBy="Bob"> <FieldData> <request_details_export_template Mod="20010101010101" IncludeInPDFExport="Yes"></request_details_export_template> <request_details_reason_for_valuatio Mod="20010101010101" IncludeInPDFExport="Yes"></request_details_reason_for_valuatio> </FieldData> <Photos Mod="20010101010101"/> <VoiceNotes/> <Drawings Mod="20010101010101"/> <FieldNotes/> </FormData> 
like image 585
Pomster Avatar asked Oct 26 '12 08:10

Pomster


People also ask

How do you change values in XML?

Change the Value of an Attribute In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have text values. The way to change the value of an attribute, is to change its text value. This can be done using the setAttribute() method or setting the nodeValue property of the attribute node.

How do I edit an existing XML file?

From the Project menu, select Add New Item. Select XML File from the Templates pane. Enter the filename in the Name field and press Add. The XML file is added to the project and opens in the XML editor.

What is attribute value XML?

The XML attribute is a part of an XML element. The addition of attribute in XML element gives more precise properties of the element i.e, it enhances the properties of the XML element. In the above syntax element_name is the name of an element which can be any name.

How do you add an element to an attribute in XML?

Get the element node and use SetAttribute to add an attribute to the attribute collection of that element. Create an XmlAttribute node using the CreateAttribute method, get the element node, then use SetAttributeNode to add the node to the attribute collection of that element.


1 Answers

There are several ways of doing this, including:

XmlAttribute formId = (XmlAttribute)xmlDoc.SelectSingleNode("//FormData/@FormId"); if (formId != null) {     formId.Value = "newValue"; // Set to new value. } 

Or this:

XmlElement formData = (XmlElement)xmlDoc.SelectSingleNode("//FormData"); if (formData != null) {     formData.SetAttribute("FormId", "newValue"); // Set to new value. } 

The SelectSingleNode method uses XPath to find the node; there is a good tutorial about XPath here. Using SetAttribute means the FormId attribute will be created if it does not already exist, or updated if it does already exist.

In this case, FormData happens to be the document's root element, so you can also do this:

xmlDoc.DocumentElement.SetAttribute("FormId", "newValue"); // Set to new value. 

This last example will only work where the node you are changing happens to be the root element in the document.

To match a specific FormId guid (it is not clear if this is what you wanted):

XmlElement formData = (XmlElement)xmlDoc.SelectSingleNode("//FormData[@FormId='d617a5e8-b49b-4640-9734-bc7a2bf05691']"); if (formData != null) {     formData.SetAttribute("FormId", "newValue"); // Set to new value. } 

Note that the select in this last example returns the FormData element and not the FormId attribute; the expression in [] brackets enables us to search for a node with a particular matching attribute.

like image 189
Polyfun Avatar answered Oct 12 '22 02:10

Polyfun