Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My XML contains data as follows:

XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE repub SYSTEM "C:\repub\Repub_V1.dtd">
<?xml-stylesheet href="C:\repub\repub.xsl" type="text/xsl"?>
<repub>
<head>
<title>xxx</title>
</head>
<body>
<sec>
<title>First Title</title>
<break name="1-1"/>
<h1><page num="1"/>First Heading</h1>
<bl>This is another text</bl>
<fig><img src="images/img_1-1.jpg" alt=""/><fc>This is a caption</fc></fig>
<p>This<br/> again is<br/> a paragraph</p>
</sec>
</body>
</repub>


It contains a < p > tag with multiple < br /> tags. I want to create a new < p > for each < br />.

What I want to achieve:

XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE repub SYSTEM "C:\repub\Repub_V1.dtd">
<?xml-stylesheet href="C:\repub\repub.xsl" type="text/xsl"?>
<repub>
<head>
<title>xxx</title>
</head>
<body>
<sec>
<title>First Title</title>
<break name="1-1"/>
<h1><page num="1"/>First Heading</h1>
<bl>This is another text</bl>
<fig><img src="images/img_1-1.jpg" alt=""/><fc>This is a caption</fc></fig>
<p>This</p>
<p>again is</p>
<p>a paragraph</p>
</sec>
</body>
</repub>


I don't know how to proceed.

What I have tried:

I am trying to approach it by using the following method:

C#
foreach (var item in xdoc.Descendants("p"))
{
	if (item.Elements("br").Count() > 0)
	{
		foreach (var br in item.Elements("br"))
		{
			//Do something
		}
	}
}
Posted
Updated 16-Apr-19 20:40pm
Comments
Christian Graus 17-Apr-19 1:18am    
I don't think this is best done with XML. You're better off just using regex or string mashing it IMO.
Primo Chalice 17-Apr-19 2:20am    
Then how shall I proceed? Is linq a good option? Please help me out

1 solution

I'd strongly recommend to read about XSLT Transformations[^]

BTW: your original xml is wrong. You can't use XDocument class, due to the fact that using Load method causes the following error: Illegal characters in the path.
 
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