My XML is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:ev="http://www.w3.org/2001/xml-events" xml:lang="de">
<head>
<title>Some Title</title>
<link rel="stylesheet" type="text/css" href="../Styles/9783748258957.css"/>
</head>
<body>
<div id="Inhalt">
<p class="toc-ch"><span class="bi">4 Main Value</span></p>
<p class="toc-h1">4.1 Child Value 1</p>
<p class="toc-h1">4.2 Child Value 2</p>
<p class="toc-h1">4.3 Child Value 3</p>
<p class="toc-h2">4.3.1 Grand-child Value</p>
<p class="toc-h2">4.3.2 Grand-child Value</p>
<p class="toc-h2">4.3.3 Grand-child Value</p>
<p class="toc-h2">4.3.4 Grand-child Value</p>
<p class="toc-h1">4.4 Child Value 4</p>
<p class="toc-h2">4.4.1 Grand-child Value</p>
<p class="toc-h2">4.4.2 Grand-child Value</p>
<p class="toc-h2">4.4.3 Grand-child Value</p>
<p class="toc-h2">4.4.4 Grand-child Value</p>
<p class="toc-h1">4.5 Child Value 5</p>
</div>
</body>
</html>
I want the XML in the following structure:
<?xml version="1.0" encoding="UTF-8"?>
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1" xml:lang="de">
<head>
<title>Some Title</title>
<link rel="stylesheet" type="text/css" href="../Styles/9783748258957.css"/>
</head>
<body>
<navMap>
<navPoint id="navPoint-01" playOrder="01"><navLabel><text>4 Main Value</text></navLabel>
<navPoint id="navPoint-02" playOrder="02"><navLabel><text>4.1 Child Value 1</text></navLabel></navPoint>
<navPoint id="navPoint-03" playOrder="03"><navLabel><text>4.2 Child Value 2</text></navLabel></navPoint>
<navPoint id="navPoint-04" playOrder="04"><navLabel><text>4.3 Child Value 3</text></navLabel>
<navPoint id="navPoint-05" playOrder="05"><navLabel><text>4.3.1 Grand-child Value</text></navLabel></navPoint>
<navPoint id="navPoint-06" playOrder="06"><navLabel><text>4.3.2 Grand-child Value</text></navLabel></navPoint>
<navPoint id="navPoint-07" playOrder="07"><navLabel><text>4.3.3 Grand-child Value</text></navLabel></navPoint>
<navPoint id="navPoint-08" playOrder="08"><navLabel><text>4.3.4 Grand-child Value</text></navLabel></navPoint></navPoint>
<navPoint id="navPoint-09" playOrder="09"><navLabel><text>4.4 Child Value 4</text></navLabel>
<navPoint id="navPoint-10" playOrder="10"><navLabel><text>4.4.1 Grand-child Value</text></navLabel></navPoint>
<navPoint id="navPoint-11" playOrder="11"><navLabel><text>4.4.2 Grand-child Value</text></navLabel></navPoint>
<navPoint id="navPoint-12" playOrder="12"><navLabel><text>4.4.3 Grand-child Value</text></navLabel></navPoint>
<navPoint id="navPoint-13" playOrder="13"><navLabel><text>4.4.4 Grand-child Value</text></navLabel></navPoint></navPoint>
<navPoint id="navPoint-14" playOrder="14"><navLabel><text>4.5 Child Value 5</text></navLabel></navPoint></navPoint>
</navMap>
</body>
</ncx>
How do I achieve this using foreach
loop?
I want to nest the elements based on the class
attribute. So, toc-ch
is the main element, toc-h1
goes under toc-ch
and toc-h2
goes under toc-h1
I am unable to make it nested. Now this is only one <div id="inhalt">
and I have multiple <div>
elements. The structure remains the same for each of the classes.
This is what I have done:
XDocument xtocdoc = XDocument.Load("epubv3TOC.xml");
XNamespace xtocNamespace = xtocdoc.Root.GetDefaultNamespace();
int navPointValue = 1;
int playOrdervalue = 1;
foreach (var value in breakslist)
{
var valueElements = value.Descendants(htmlNamespace + "p").ToList();
foreach (var values in valueElements)
if (values.Attribute("class") != null)
{
if (values.Attribute("class") != null && values.Attribute("class").Value.Contains("ch"))
{
xtocdoc.Descendants(xtocNamespace + "navMap").FirstOrDefault(de => de != null)
.Add(new XElement(xtocNamespace + "navPoint", new XAttribute("id", "navPoint-" + navPointValue)
, new XAttribute("playOrder", playOrdervalue)
, new XElement(xtocNamespace + "navLabel"
, new XElement(xtocNamespace + "text", "ch"))));
continue;
}
else if (values.Attribute("class") != null && values.Attribute("class").Value.Contains("h1"))
{
xtocdoc.Descendants(xtocNamespace + "navMap").FirstOrDefault(de => de != null)
.Elements(xtocNamespace + "navPoint")
.FirstOrDefault(el => el != null)
.Add(new XElement(xtocNamespace + "navPoint", new XAttribute("id", "navPoint-" + navPointValue)
, new XAttribute("playOrder", playOrdervalue)
, new XElement(xtocNamespace + "navLabel"
, new XElement(xtocNamespace + "text", "h1"))));
continue;
}
else if (values.Attribute("class") != null && values.Attribute("class").Value.Contains("h2"))
{
xtocdoc.Descendants(xtocNamespace + "navMap").FirstOrDefault(de => de != null)
.Elements(xtocNamespace + "navPoint")
.FirstOrDefault(el => el != null)
.Add(new XElement(xtocNamespace + "navPoint", new XAttribute("id", "navPoint-" + navPointValue)
, new XAttribute("playOrder", playOrdervalue)
, new XElement(xtocNamespace + "navLabel"
, new XElement(xtocNamespace + "text", "h2"))));
continue;
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…