我有一些xml文件,其中有文章部分的节点,如
MAIN SECTIONS:
<section id="sec1">
<section id="sec2">
...
有各自的子部分,如
1ST SUBSECTIONS:
<section id="sec1a">
<section id="sec1b">
...
然后是
这样的子部分2ND SUBSECTIONS:
<section id="sec1a1">
<section id="sec1a2">
...
&安培;他们的小节
FINALLY 3RD SUBSECTIONS:
<section id="sec1a1a">
<section id="sec1a1b">
...
当特定类型的一部分结束并开始一个新部分时,将放置这些部分的结束节点 e.x。
<section id="sec1">
<p>....</p>
</section>
<section id="sec2">
or
<section id="sec1">
<p>....</p>
<section id="sec1a">
<p>...</p>
</section>
<section id="sec2">
or
<section id="sec1">
<p>....</p>
<section id="sec1a">
<section id="sec1a1">
<p>...</p>
</section>
</section>
</section>
<section id="sec2">
依旧......
但我的文件的id属性值都混淆了..
有没有办法使用结束节点</section>
作为标识符来格式化它。?
示例文件内容
<section id="sec1">
<label>1.</label>
<title>INTRODUCTION</title>
<p>Despite the large number of scientific papers devoted to the heart <sup><xref ref-type="bibr" rid="c1">1</xref>–<xref ref-type="bibr" rid="c2">2</xref></sup>, this time to think both home and foreign authors there are many different controversial issues. They are associated with morphological structural features of valvular heart disease and its individual structural components: valves, tendon strings mastoid muscle and fibrous ring <sup><xref ref-type="bibr" rid="c1">1</xref>–<xref ref-type="bibr" rid="c2">2</xref></sup>.</p>
<section id="sec2">
<title>INTRO</title>
<p>All structures are large valvular morphofunctional load.</p>
<p>According to the classification of tendon strings of the heart they are classified as boundary, i.e, those that are attached to the edges of the leaves, leaf, spot fixing being lower surface of leaf valve (facing the cavity of the ventricle) and abnormally arranged strings.</p>
<p>Thus detailed knowledge of the structural features of normal tendon strings.</p>
</section>
</section>
<section id="sec3">
<label>2.</label>
<title>THE MORPHOLOGY OF CHONDRAE TENDIANEAE</title>
<section id="sec2a">
<title>THE MORPHOLOGY</title>
<p>According to the macroscopic study of tendon strings newborns and infants</p>
<section id="sec5a1">
<title>THE OML</title>
<p>Total number of tendon strings are attached to the cusps of atrioventricular valves of the heart in both age groups ranged from 30 to 80.</p>
</section>
</section>
</section>
预期输出
<section id="sec1">
<label>1.</label>
<title>INTRODUCTION</title>
<p>Despite the large number of scientific papers devoted to the heart <sup><xref ref-type="bibr" rid="c1">1</xref>–<xref ref-type="bibr" rid="c2">2</xref></sup>, this time to think both home and foreign authors there are many different controversial issues. They are associated with morphological structural features of valvular heart disease and its individual structural components: valves, tendon strings mastoid muscle and fibrous ring <sup><xref ref-type="bibr" rid="c1">1</xref>–<xref ref-type="bibr" rid="c2">2</xref></sup>.</p>
<section id="sec1a">
<title>INTRO</title>
<p>All structures are large valvular morphofunctional load.</p>
<p>According to the classification of tendon strings of the heart they are classified as boundary, i.e, those that are attached to the edges of the leaves, leaf, spot fixing being lower surface of leaf valve (facing the cavity of the ventricle) and abnormally arranged strings.</p>
<p>Thus detailed knowledge of the structural features of normal tendon strings.</p>
</section>
</section>
<section id="sec2">
<label>2.</label>
<title>THE MORPHOLOGY OF CHONDRAE TENDIANEAE</title>
<section id="sec2a">
<title>THE MORPHOLOGY</title>
<p>According to the macroscopic study of tendon strings newborns and infants</p>
<section id="sec2a1">
<title>THE OML</title>
<p>Total number of tendon strings are attached to the cusps of atrioventricular valves of the heart in both age groups ranged from 30 to 80.</p>
</section>
</section>
</section>
答案 0 :(得分:2)
您需要使用递归算法。见下面的代码。我不确定你发布的小样本的编号方案。我的编号有点不同。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test1.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement root = doc.Root;
RecusiveParse(root, 1, "sec");
}
static void RecusiveParse(XElement parent, int level, string parentId)
{
int index = 1;
foreach (XElement child in parent.Elements("section"))
{
string id = "";
if (level % 2 == 0)
{
string prefix = ((char)('a' + (index - 1))).ToString();
id = parentId + prefix;
}
else
{
id = parentId + index.ToString();
}
child.Attribute("id").Value = id;
RecusiveParse(child, level + 1, id);
index++;
}
}
}
}