Click here to Skip to main content
16,022,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to replace all the tag values in C#

Consider I have an XML as
XML
<Root><FName>Test</FName><LName>User</LName><DOB>20100606</DOB></Root>


need to replace FName to FirstName, LName to LastName and DOB to DateOfBirth in C#

There are other elements in the XML as well which needs to be untouched and remain as is.

What I have tried:

Tried using replace, but trying to find a better way for this.
Posted
Updated 7-Apr-21 22:18pm

The fact that you have said that you have tried using Replace suggests that you are just treating this XML as a string.

There are many libraries for handling XML files - use one of those and amending the value of any tag becomes trivial.

Some suggestions to get you started, from CodeProject
Working with XML[^]
Using XML in C# in the simplest way[^]
Reading and Writing XML in C#/VB.Net[^]

And from elsewhere
XmlDocument Class (System.Xml) | Microsoft Docs[^]
Introduction to XML with C# - The complete C# tutorial[^]
C# XmlReader - reading XML in C# with XmlReader[^]

Choose the library that suits you and the style of writing that suits you. Follow the instructions and ... there you go!
 
Share this answer
 
This is simple using LINQ to XML:
C#
XDocument doc = XDocument.Load("...path to your XML file...");

doc.Root.Element("FName").Name = "FirstName";
doc.Root.Element("LName").Name = "LastName";
doc.Root.Element("DOB").Name = "DateOfBirth";
 
Share this answer
 

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