Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
how can I select and change the value "Entwicklung" (bold, unterline) in this XML in C#?
Thank you
Regards
Nicole

XML
<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <!-- Pfad zu log-Verzeichnis  -->
  <variable name="logDir"  value="${specialfolder:folder=ApplicationData}/BAMF/Maris/logs"/>
  <variable name="logFile" value="${shortdate}.log"/>
  <variable name="umgebung" value="Entwicklung"/>
</nlog>
Posted

You could use Xelement class to do that:
http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement_methods.aspx[^]

As shown here using ReplaceWith:
http://stackoverflow.com/questions/5820143/how-can-i-update-replace-an-element-of-an-xelement-from-a-string[^]

XML
// input would be your edited XML, this is just sample data to illustrate
string input = @"<Artist Name=""Pink Floyd"">
  <BandMembers>
    <Member>Nick Mason</Member>
    <Member>Syd Barret</Member>
    <Member>David Gilmour</Member>
    <Member>Roger Waters</Member>
    <Member>Richard Wright</Member>
  </BandMembers>
  <Category>Favorite band of all time</Category>
</Artist>";


C#
var replacement = XElement.Parse(input);
var pinkFloyd = xml.Elements("Genre")
                   .Where(e => e.Attribute("Name").Value == "Rock")
                   .Elements("Artist")
                   .Single(e => e.Attribute("Name").Value == "Pink Floyd");

pinkFloyd.ReplaceWith(replacement);
Console.WriteLine(xml);
 
Share this answer
 
This needs to be cleaned up but you can access it as follows :

var keyvalues = from item in document.Descendants()
                select item;

var xAttribute = keyvalues.ElementAt(3).Attribute("value");
if (xAttribute != null)
{
    var configvalue = xAttribute.Value;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900