Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change an XML node value

Tags:

c#

xml

I have an xml document that looks like this

<?xml version="1.0"?>
<XML>
    <VIDEO>
        <WIDTH>800</WIDTH>
        <HEIGHT>600</HEIGHT>
        <COLORBITS>32</COLORBITS>
        <GAMMA>255</GAMMA>
        <FULLSCREEN>TRUE</FULLSCREEN>
        <REFLECTION>true</REFLECTION>
        <LIGHTMAP>true</LIGHTMAP>
        <DYNAMICLIGHT>true</DYNAMICLIGHT>
        <SHADER>true</SHADER>
        <CHARACTORTEXTURELEVEL>0</CHARACTORTEXTURELEVEL>
        <MAPTEXTURELEVEL>0</MAPTEXTURELEVEL>
        <EFFECTLEVEL>0</EFFECTLEVEL>
        <TEXTUREFORMAT>1</TEXTUREFORMAT>
        <NHARDWARETNL>false</NHARDWARETNL>
    </VIDEO>    
</XML>

I want to change the value of the "MAPTEXTURELEVEL" node from 0 to 6 using the checked statement of a checkbox in a C# application, but I really have no idea of how I can do it.

like image 951
Derezzed Avatar asked Dec 29 '11 00:12

Derezzed


People also ask

How do you change a value in XML?

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.

What is node value in XML?

The nodeValue property is used to get the text value of a node. The getAttribute() method returns the value of an attribute.

How do I select a specific node in XML?

To find nodes in an XML file you can use XPath expressions. Method XmlNode. SelectNodes returns a list of nodes selected by the XPath string. Method XmlNode.


1 Answers

I don't have VS to test it, but it should be something like this using LINQ to XML:

var doc = XDocument.Load("video.xml");
doc
    .Element("XML")
    .Element("VIDEO")
    .SetElementValue("MAPTEXTURELEVEL", 6);
doc.Save("video_modified.xml");

Hope it helps!

like image 68
alf Avatar answered Sep 30 '22 07:09

alf