Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello!

This is my XML:

XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE donhag SYSTEM "C:\donhag\donhag_V1.dtd">
<?xml-stylesheet href="C:\donhag\donhag.xsl" type="text/xsl"?>
<donhag>
<head>
<title>TAKEABREAK</title>
</head>
<body>
<sec>
<title>Editorial</title>
<break name="5-1"/>
<h1><page num="5"/>This is a heading</h1>
<h3>This is a sub-heading</h3>
<p>This is a paragraph</p>
<fig>This is an image</fig>
</sec>
<sec>
<title>Your Lives</title>
<break name="6-1"/>
<h1><page num="6"/>This is a heading</h1>
<h3>This is a sub-heading</h3>
<p>This is a paragraph</p>
<fig>This is an image</fig>
</sec>
<sec>
<title>Your Prize Puzzles</title>


Here there are three <title> tags. I want to split the XML into multiple xml files where each XML file will contain the data from <title> to the next <title>.

So the output will be:

XML1:

XML
<title>TAKEABREAK</title>
</head>
<body>
<sec>
<title>Editorial</title>
<break name="5-1"/>
<h1><page num="5"/>This is a heading</h1>
<h3><b>This is a sub-heading</b></h3>
<p>This is a paragraph</p>
<fig>This is an image</fig>
</sec>
<sec>


XML 2:

XML
<title>Your Lives</title>
<break name="6-1"/>
<h1><page num="6"/>This is a heading</h1>
<h3><b>This is a sub-heading</b></h3>
<p>This is a paragraph</p>
<fig>This is an image</fig>
</sec>
<sec>


Please help.

Regards
Aman

What I have tried:

I have read the XML and I need to split it using Xdocument.
Posted
Updated 3-Feb-20 5:02am
v2
Comments
Maciej Los 16-Jan-19 3:22am    
A desired XML is not well fomed. There's no a root node.
Primo Chalice 16-Jan-19 3:56am    
So, is it not possible without <root> because these are custom XML files so we have our own root tags. <donhag> in this case.

Regards
CHill60 16-Jan-19 3:42am    
The "What I have tried" section is where you put the code you have already tried to write so that we can analyse it and fix it

1 solution

Try this:

C#
string sSourceFileName  = @"YourXmlFileNameHere.xml";
XDocument xdoc = XDocument.Load(sSourceFileName);
List<XElement> titles = xdoc.Descendants("title").ToList();
foreach(XElement xele in titles)
{
    XDocument xd = new XDocument(
    new XElement("donhag",
        new XElement(xele.Name, xele.Value),
        xele.ElementsAfterSelf()));
    string sNewFileName = Path.Combine(@"D:\MyXmlData\", xele.Value.ToString().Replace(" ", "") + ".xml");
    xd.Save(sNewFileName);
}


Result - 4 files:
====
D:\MyXmlData\TAKEABREAK.xml
'----
<donhag>
  <title>TAKEABREAK</title>
</donhag>

====
D:\MyXmlData\Editorial.xml
'----
<donhag>
  <title>Editorial</title>
  <break name="5-1" />
  <h1>
    <page num="5" />This is a heading</h1>
  <h3>This is a sub-heading</h3>
  <p>This is a paragraph</p>
  <fig>This is an image</fig>
</donhag>

====
D:\MyXmlData\YourLives.xml
'----
<donhag>
  <title>Your Lives</title>
  <break name="6-1" />
  <h1>
    <page num="6" />This is a heading</h1>
  <h3>This is a sub-heading</h3>
  <p>This is a paragraph</p>
  <fig>This is an image</fig>
</donhag>

====
D:\MyXmlData\YourPrizePuzzles.xml
'----
<donhag>
  <title>Your Prize Puzzles</title>
  <break name="7-1" />
  <h1>
    <page num="7" />This is a heading</h1>
  <h3>This is a sub-heading</h3>
  <p>This is a paragraph</p>
  <fig>This is an image</fig>
</donhag>
 
Share this answer
 
v2

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