如何在C#中清除XML文件中的旧值? [英] How do I clear the old values from XML file in C#?

查看:106
本文介绍了如何在C#中清除XML文件中的旧值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!



我正在尝试创建多个XMl文件,但是在创建新文件时,还会添加先前XML中的值。我希望每次创建新的XML时都要添加新值。



主XML:



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 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>





将此视为我的XML,我的输出应为:



section_01.xml -



Considering this as my XML, my output should be:

section_01.xml -

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





section_02.xml -



section_02.xml -

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







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





我得到的是:



section_01.xml -



What I am getting:

section_01.xml -

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





section_02 .xml -



section_02.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 -



section_03.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:

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

推荐答案

不需要GroupBy。实际上,这里有一些问题。



如果不熟悉,请不要尝试一次完成所有操作。最好打破所需的部件,然后收紧代码。



这样的东西......

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...
// 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.


这篇关于如何在C#中清除XML文件中的旧值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆