Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
Hello!

I am trying to create multiple XMl files but when a new file is being created, the values from the previous XML is also being added. I want new values to be added each time a new XML is created.

Main XML:

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">
<heading><page num="1"/>First Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
<sec>
<title>Second Title</title>
<break name="2-1">
<heading><page num="1"/>Second Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
<sec>
<title>First Title</title>
<break name="3-1">
<heading><page num="1"/>Third Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
<sec>
<title>Third Title</title>
<break name="4-1">
<heading><page num="1"/>Fourth Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
<break name="5-1">
<heading><page num="1"/>Fifth Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
</body>
</repub>


Considering this as my XML, my output should be:

section_01.xml -
XML
<root>........
<division-id id="preview_1" class="sectionArticleBlockOdd1" />
<division-id id="preview_2" class="sectionArticleBlockOdd2" />
......</root>


section_02.xml -
XML
<root>........
<division-id id="preview_3" class="sectionArticleBlockOdd3" />
......</root>


XML
section_03.xml - 
<root>........
<division-id id="preview_4" class="sectionArticleBlockOdd4" />
<division-id id="preview_5" class="sectionArticleBlockOdd5" />
......</root>


What I am getting:

section_01.xml -
XML
<root>........
<division-id id="preview_1" class="sectionArticleBlockOdd1" />
<division-id id="preview_2" class="sectionArticleBlockOdd2" />
......</root>


section_02.xml -
XML
<root>........
<division-id id="preview_1" class="sectionArticleBlockOdd1" />
<division-id id="preview_2" class="sectionArticleBlockOdd2" />
<division-id id="preview_3" class="sectionArticleBlockOdd3" />
......</root>


section_03.xml -
XML
<root>........
<division-id id="preview_1" class="sectionArticleBlockOdd1" />
<division-id id="preview_2" class="sectionArticleBlockOdd2" />
<division-id id="preview_3" class="sectionArticleBlockOdd3" />
<division-id id="preview_4" class="sectionArticleBlockOdd4" />
<division-id id="preview_5" class="sectionArticleBlockOdd5" />
......</root>


Please help.

What I have tried:

C#
var sectionlist = xdoc.Descendants("sec").GroupBy(b => b.Element("title").Value).Select(b => b).ToList(); // xdoc is the main XML
foreach (var uniquesections in sectionlist)
    {
        var sectionbreakcount = uniquesections.Descendants("break").ToList();
		xsecdoc.Root.Element(ns + "head").Element(ns + "title").Value = ""; //xsecdoc is the output XML that will be created for every section found
		xsecdoc.Root.Element(ns + "head").Element(ns + "title").Add(uniquesections.Key);
		xsecdoc.Root.Element(ns + "body").Descendants().FirstOrDefault(el => (string)el.Attribute("class") == "sectionName").Value = "";
		xsecdoc.Root.Element(ns + "body").Descendants().FirstOrDefault(el => (string)el.Attribute("class") == "sectionName").Add(uniquesections.Key);
        foreach(var article in sectionbreakcount)
            {
                articlecount++;
                xsecdoc.Root.Element(ns + "body").Add(new XElement(ns + "division-id", new XAttribute("id", "preview_" + articlecount, new XAttribute("class", "sectionArticleBlockOdd" + articlecount)));
            }
            File.WriteAllText(opsFolder + "\\" + "section_0" + (sectionloopcount++) + ".xml", XmlEncode(xsecdoc));
	}
Posted
Updated 23-Feb-19 23:25pm
Comments
Richard Deeming 26-Feb-19 12:49pm    
File.WriteAllText(opsFolder + "\\" + "section_0" + (sectionloopcount++) + ".xml", XmlEncode(xsecdoc));

No need to write your own method to save an XDocument to a file. Just use the Save method[^]:
xsecdoc.Save(opsFolder + "\\" + "section_0" + (sectionloopcount++) + ".xml");

1 solution

GroupBy is not required. Actually, there are a few issues here.

When unfamiliar, don't try and do it all at once. The best to break out the parts required, then tighten up the code.

Something like this...
C#
// break out each section
foreach (var uniqueSection in xdoc.Descendants("sec"))
{
    // process Group here ...
    var title = uniqueSection.Element("title")?.Value;

    foreach (var sectionBreak in uniqueSection.Descendants("break"))
    {
        // process section
        var heading = sectionBreak.Element("heading")?.Value;
        var bl = sectionBreak.Element("bl")?.Value;
        var body = sectionBreak.Descendants("p").Select(x => x.Value);
    }
}

Now, set a breakpoint and step through the code and check that the correct parts are being selected at each step.

Once you know that the code is pulling the correct parts, now you are ready to write out to the separate files.
 
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