Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello!

I am developing an application to convert text in XML files. I have already created a part of it, where I will enter the folder path and it will load all the xml files in that folder.

Now what I want is that all the xml files have texts and I want something like the following:

Text in XML files

<image>
<caption>This is a caption.</caption>
<credits>This is a test sentence.</credits>
</image>


Required replacement

<credits>
This is a test sentence
</credits>

<image>
<caption>This is a caption.</caption>
</image>


Basically I want to separate the <credits> tag from inside the tag an keep the rest as it is. All the changes will be made in the imported xml files from the folder path. I have displayed the list of file in a ListView.

Please help.

What I have tried:

I have created a basic program to import multiple xml files.
Posted
Updated 13-Jan-19 23:19pm
v2

The simplest and probably fastest way might be to do a simple file based replace, you can read a file into a List<string> with File.ReadAllLines().
Then loop through all lines and replace the <credits> lines by deleting them and inserting at the new position.

Another way would be to use the XElement class, see: XElement Class (System.Xml.Linq) | Microsoft Docs[^]
and: Serializing XML Trees (C#) | Microsoft Docs[^]
This should get you started:

test.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
  <image>
    <caption>This is a caption.</caption>
    <credits>This is a test sentence.</credits>
  </image>
  <image>
    <caption>This is a caption 2.</caption>
    <credits>This is a test sentence 2.</credits>
  </image>
</root>

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

string fileName = @"test2.xml";
var xmlSource = XElement.Load(fileName);
var xList = xmlSource.Elements("image");

foreach (var x in xList)
{
    if (x == null)
    {
        continue;
    }

    var credits = x.Element("credits");

    if (credits != null)
    {
        Debug.Print(credits?.Value);
        var c = credits;
        c.Remove();
    }
}

xmlSource.Save(fileName + "new");
 
Share this answer
 
v5
Comments
Primo Chalice 14-Jan-19 2:21am    
Hello!

Could you please send me a snippet of the same using XDocument?

Please

Regards
Aman
Maciej Los 14-Jan-19 5:19am    
5ed!
It's quite simple. All you have to do is to:
1. loop through the collection of files in folder by using Directory.GetFiles Method (System.IO) | Microsoft Docs[^]
2. load each file (XDocument.Load Method (System.Xml.Linq) | Microsoft Docs[^])
3. find and remove "caption" nodes
4. and finally insert them in a root node:
C#
string[] files = Directory.GetFiles(@"YourPathhere", "*.xml")
foreach(string sFileName in files)
XDocument xdoc = XDocument.Load(sFileName);
List<XElement> nodes2move = xdoc.Descendants("credits").ToList();
foreach(var n in nodes2move)
{
	n.Remove();	
	xdoc.Root.AddFirst(n);
}
xdoc.Save(sFileName);


A new content of document should look like:
XML
<?xml version="1.0" encoding="utf-8"?>
<mydata>
  <credits>This is a test sentence.</credits>
  <image>
    <caption>This is a caption.</caption>
  </image>
</mydata>


For further details, please see:
XContainer.Elements Method (System.Xml.Linq) | Microsoft Docs[^]
XNode.Remove Method (System.Xml.Linq) | Microsoft Docs[^]
XContainer.AddFirst Method (System.Xml.Linq) | Microsoft Docs[^]
 
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