2

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!

3 Answers 3

3

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. :)

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

5 Comments

thank you very much.. i got it!! :-).. only thing is once the value gets set to 0000000, i want to come out of the loop. but break here, actually take the control to the last line in the script and not immediate line after the loop.
Awesome! glad I could help. If you think it was good you should pay it forward and +1 the answer so others can see. :) I just went through this same problem so I just had to respond. hahahaha
The arrows on left side. Cheers.
thanks!! I did it.. Somehow not able to save the changes in the file.. when I check root variable in debug mode values are still not updated
Check the file location, would your system have permission to edit a file there? Also is it a service ? That would mess it up for sure. I know how to fix that as well though
0

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

        }


    }


}

1 Comment

i cannot use linq XML. i am trying using XMLdocument, XML element but I am missing something..
0

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

2 Comments

for me somehow Xelement, Xdocument are disabled and i am only able to use XMLelement, XMLDocument etc.
Not a problem, my second answer that I just posted solves that. I'm keeping this answer up for others benefit. It goes nicely beside jdwengs sweet linq statement.

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.