在c#中生成文件中某个节点的属性值?

在c#中生成文件中某个节点的属性值?,c#,xml,C#,Xml,我有一些xml文件,其中有一篇文章的节点,比如 MAIN SECTIONS: <section id="sec1"> <section id="sec2"> ... 主要章节: ... 各小节如下: 1ST SUBSECTIONS: <section id="sec1a"> <section id="sec1b"> ... 2ND SUBSECTIONS: <section id="sec1a1"> <section id=

我有一些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">
...
1小节:
...
然后细分为

1ST SUBSECTIONS:
<section id="sec1a">
<section id="sec1b">
...
2ND SUBSECTIONS:
<section id="sec1a1">
<section id="sec1a2">
...
2小节:
...
&其分部

FINALLY 3RD SUBSECTIONS:
<section id="sec1a1a">
<section id="sec1a1b">
...
最后第三小节:
...
当特定类型的节结束并开始新的节时,将放置这些节的结束节点 e、 十,


等等。。。 但是我的文件的id属性值都混淆了。。 是否有方法使用结束节点
作为标识符对其进行格式化

示例文件内容

<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>&#x2013;<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>&#x2013;<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>

1.
介绍
尽管有大量关于心脏的科学论文1–;这一次认为国内外作家都存在许多不同的争议性问题。它们与瓣膜性心脏病的形态结构特征及其个体结构成分有关:瓣膜、腱索、乳突肌和纤维环1–;二,

简介 所有结构均为大的瓣膜形态功能负荷

根据心脏腱索的分类,它们被划分为边界,即附在叶片边缘的腱索、叶片、斑固定物(叶瓣下表面(面向心室腔)和排列异常的腱索

从而详细了解正常钢筋束的结构特征

2. 蔓藤球茎的形态 形态 根据新生儿和婴儿肌腱的宏观研究

OML 在两个年龄组(30-80岁)的心脏房室瓣尖上附着的腱索总数

预期产量

<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>&#x2013;<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>&#x2013;<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>

1.
介绍
尽管有大量关于心脏的科学论文1–;这一次认为国内外作家都存在许多不同的争议性问题。它们与瓣膜性心脏病的形态结构特征及其个体结构成分有关:瓣膜、腱索、乳突肌和纤维环1–;二,

简介 所有结构均为大的瓣膜形态功能负荷

根据心脏腱索的分类,它们被划分为边界,即附在叶片边缘的腱索、叶片、斑固定物(叶瓣下表面(面向心室腔)和排列异常的腱索

从而详细了解正常钢筋束的结构特征

2. 蔓藤球茎的形态 形态 根据新生儿和婴儿肌腱的宏观研究

OML 在两个年龄组(30-80岁)的心脏房室瓣尖上附着的腱索总数


您需要使用递归算法。请参阅下面的代码。我不确定你的编号方案与你张贴的小样本。我的编号有点不同

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++;
            }
        }
    }
}

这个问题有点令人困惑。不清楚问题是在序列化还是反序列化时出现的。您正在尝试转换现有xml吗?@KernelMode是的,我正在尝试转换现有xml。.数字模式类似于Main section
id=sec1,id=sec2
,然后是第一小节sec1
sec1a
,第二小节sec1
sec1b
…依此类推,
sec1a/sec1b
的第一小节将是
sec1a1、sec1a2/sec1b1、sec1b2
等等。。如果它们存在,那么在
之间也可能没有子节。希望我说得更清楚一点:)代码不起作用,因为格式良好的xml只有一个根标记。您的代码在根级别有多个标记。我以为你有根标签。我还认为最后一个id应该是sec2ab1,我尝试添加一个根元素,但它仍然没有做任何事情…最后一个id应该是2a1,还有,您是否得到关闭节点的逻辑?将现有输入的开头和结尾放在应该会更新文档。我尝试了。但是它没有更新文档…代码只是运行,没有任何错误,但是文件没有更改。。。