简体   繁体   中英

How to update node values in XML using C#

I have to update an original XML file with modified values. Below is my sample XML file:

<request>
<facility>
<alternateIDs>
            <alternateID code="ALT8">11111111</alternateID>
            <alternateID code="ALT12">111111111</alternateID>
            <alternateID code="ALT">1111111111</alternateID>
            <alternateID code="ALT1">11111111</alternateID>
            <alternateID code="ALT9">11111111</alternateID>
            <alternateID code="ALT3">111111111</alternateID>
</alternateIDs>
</facility>
</request>

Now I want to look for alternateID code="ALT" and change its value to 00000000 . My final file should look like:

<request>
<facility>
<alternateIDs>
            <alternateID code="ALT8">11111111</alternateID>
            <alternateID code="ALT12">111111111</alternateID>
            <alternateID code="ALT">00000000</alternateID>
            <alternateID code="ALT1">11111111</alternateID>
            <alternateID code="ALT9">11111111</alternateID>
            <alternateID code="ALT3">111111111</alternateID>
</alternateIDs>
</facility>
</request>

How can I achieve this using XElement and XAttributes ? I am not familiar with XML and C#. Any help is appreciated!

Got it!

public static void ReplaceCode()
     {
        var root = new XmlDocument();
         root.Load(@"C:\data.xml");

        foreach (XmlNode e in root.GetElementsByTagName("alternateID"))
        {
            if (e.Attributes["code"].Value.Equals("ALT"))
            {
                e.FirstChild.Value = "00000000"; // FirstChild because the inner node is actually the inner text, yeah XmlNode is weird.
                break;
            }
        }
        root.Save(@"C:\data.xml");
    }

Ask me anything about it and I can clarify. :)

Try this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication34
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement alt = doc.Descendants("alternateID").Where(x => (string)x.Attribute("code") == "ALT").FirstOrDefault();
            alt.Value = "00000000";

        }


    }


}

Here is a non Linq way of doing it, although linq is much cleaner if you know how to read it.

public static void ReplaceCode()
     {
        var root = XElement.Load(@"C:\data.xml");
        foreach (var e in root.Descendants("alternateID"))
        {
            if (!e.Attribute("code").Value.Equals("ALT")) continue;
            e.Value = "00000000";
            break;
        }
        root.Save(@"C:\data.xml");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM