如何在C#中的多个XML文件中搜索XElement属性值?

时间:2019-06-06 07:47:00

标签: c# .net xml linq-to-xml

我的XML内容类似于:

<p class="toc-title"><a id="page_5"></a>Inhoud</p>
<p class="toc-fm"><a href="___.html#foreword">Woord vooraf</a></p>
<p class="toc-fm"><a href="___.html#Inleiding">Inleiding: wat is verslaving?</a></p>
<p class="toc-ch"><a href="___.html#Chapter01"><span class="toc-num">1.&#x00A0;</span>Verslaving en leegte</a></p>
<p class="toc-h1"><a href="___.html#h1_1"><i>Eten</i></a></p>
<p class="toc-ch"><a href="___.html#Chapter02"><span class="toc-num">2.&#x00A0;</span>Zelfafwijzing en zelfveroordeling</a></p>
<p class="toc-h1"><a href="___.html#h1_2"><i>Social media</i></a></p>
<p class="toc-ch"><a href="___.html#Chapter03"><span class="toc-num">3.&#x00A0;</span>Beperking van je vrijheid</a></p>
<p class="toc-h1"><a href="___.html#h1_3"><i>Macht, aanzien en bezit</i></a></p>
<p class="toc-h1"><a href="___.html#h1_4"><i>Pornoverslaving</i></a></p>
<p class="toc-ch"><a href="___.html#Chapter04"><span class="toc-num">4.&#x00A0;</span>Verslaving en het brein</a></p>
<p class="toc-h1"><a href="___.html#h1_5"><i>Roken</i></a></p>
<p class="toc-ch"><a href="___.html#Chapter05"><span class="toc-num">5.&#x00A0;</span>Risicofactoren en beschermende factoren voor verslaving: uitdagingen voor de sociale en kerkelijke omgeving</a></p>
<p class="toc-h1"><a href="___.html#h1_6"><i>Gamen</i></a></p>
<p class="toc-ch"><a href="___.html#Chapter06"><span class="toc-num">6.&#x00A0;</span>Verslaving als psychiatrische stoornis: psychiatrische klachten in combinatie met verslavingsproblematiek</a></p>
<p class="toc-h1"><a href="___.html#h1_7"><i>Medicijnverslaving</i></a></p>
<p class="toc-ch"><a href="___.html#Chapter07"><a id="page_6"></a><span class="toc-num">7.&#x00A0;</span>Verslaving in het gezin</a></p>
<p class="toc-h1"><a href="___.html#h1_8"><i>Afhankelijkheid</i></a></p>
<p class="toc-ch"><a href="___.html#Chapter08"><span class="toc-num">8.&#x00A0;</span>Verslaving en geloof: wetenschappelijk onderzoek</a></p>
<p class="toc-h1"><a href="___.html#h1_9"><i>Alcohol</i></a></p>
<p class="toc-ch"><a href="___.html#Chapter09"><span class="toc-num">9.&#x00A0;</span>Is verslaving een ziekte?</a></p>
<p class="toc-h1"><a href="___.html#h1_10"><i>Drugs</i></a></p>
<p class="toc-ch"><a href="___.html#Chapter10"><span class="toc-num">10.&#x00A0;</span>Herstel in vier relaties: behandeling en begeleiding bij verslavingsproblematiek</a></p>
<p class="toc-h1"><a href="___.html#h1_11"><i>Woede en gekrenktheid</i></a></p>
<p class="toc-ch"><a href="___.html#Chapter11"><span class="toc-num">11.&#x00A0;</span>De man op de bank: aandachtspunten en valkuilen voor het pastoraat</a></p>
<p class="toc-h1"><a href="___.html#h1_12"><i>Gokken</i></a></p>
<p class="toc-bm"><a href="___.html#Literatuur">Geraadpleegde literatuur</a></p>
<p class="toc-bm"><a href="___.html#Personalia">Personalia</a></p>

我有n个XML文件,从15到350个文件不等。我想在字符串中获取每个<a href>属性值,并在所有XML文件(包括该XML文件)中的Attribute("id")中进行搜索。如果找到,我将替换___并将XML文件名放在找到的地方。

我已完成以下操作,但速度非常慢。您能帮我更快一点吗?

这是我的代码:

string pathFolder = TextBoxPath.Text;
DirectoryInfo directoryInfo = new DirectoryInfo(pathFolder);
string pathParent = directoryInfo.Parent.FullName;
string textFolder = Path.Combine(pathParent, "Text");
Regex filePattern = new Regex("\\d{13}");
if (ePUBv2CheckBox.IsChecked == true)
{
    fileFolder = pathFolder;
}
else
{
    fileFolder = textFolder;
}
string getISBNFile = Directory.GetFiles(fileFolder, "*.css", SearchOption.AllDirectories)
                           .Where(fileName => filePattern.IsMatch(Path.GetFileNameWithoutExtension(fileName))).FirstOrDefault();
string pathFileNameFolder = Path.GetFileNameWithoutExtension(getISBNFile);
List<string> getAllChapters = new List<string>();
List<string> getAllChaptersButThis = new List<string>();
if (ePUBv2CheckBox.IsChecked == true)
{
    getAllChapters = Directory.GetFiles(fileFolder, "*.html")
                                .Where(name => !(Path.GetFileName(name).Contains(pathFileNameFolder) || Path.GetFileName(name).ToLower().Contains("cover")))
                                .ToList();
}
else
{
    getAllChapters = Directory.GetFiles(fileFolder, "*.xhtml")
                                .Where(name => !(Path.GetFileName(name).ToLower().Contains("cover")))
                                .ToList();
}
foreach (var eachChapter in getAllChapters)
{
    string nameForSaving = Path.GetFileName(eachChapter);
    XDocument newChapter = XDocument.Load(eachChapter);
    XNamespace newNamespace = newChapter.Root.GetDefaultNamespace();
    List<XElement> hrefAttributes = newChapter.Descendants(newNamespace + "a")
                                       .Where(at => at.Attribute("href") != null
                                                && (at.Attribute("href").Value.Contains(".xhtml")
                                                 || at.Attribute("href").Value.Contains(".html")))
                                       .ToList();
    if (hrefAttributes.Count() > 0)
    {
        foreach (XElement hrefUnique in hrefAttributes)
        {
            string hrefValue = hrefUnique.Attribute("href").Value;
            string hrefLink = hrefValue.Substring(hrefValue.IndexOf("#") + 1);
            foreach (var anotherChapter in getAllChapters)
            {
                string fileName = Path.GetFileNameWithoutExtension(anotherChapter);
                fileName = fileName.Substring(fileName.IndexOf("_") + 1).Replace("_", String.Empty);
                XDocument temporaryDocument = XDocument.Load(anotherChapter);
                foreach (XElement breakChapter in temporaryDocument.Descendants())
                {
                    List<XElement> getListID = breakChapter.Descendants().Where(at => at.Attribute("id") != null
                                                                                       && at.Attribute("id").Value.Equals(hrefLink, StringComparison.InvariantCultureIgnoreCase))
                                                                        .ToList();
                    if (getListID.Count() > 0 || fileName.Equals(hrefLink, StringComparison.InvariantCultureIgnoreCase))
                    {
                        string getChapterFile = getAllChapters.FirstOrDefault(ch => Path.GetFileName(ch)
                                                        .Contains(fileName));
                        hrefUnique.SetAttributeValue("href", Path.GetFileName(getChapterFile) + "#" + hrefLink);
                        break;
                    }
                }
                newChapter.Save(fileFolder + "\\" + nameForSaving);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果目标只是更新所有具有<a>且具有href属性的"___"元素,则只需搜索这些元素并更新属性值。我不确定其余的代码在做什么,但是您所需要做的只是这样:

void UpdateFile(string path)
{
    var doc = XDocument.Load(path);
    var filename = Path.GetFileNameWithoutExtension(path);
    foreach (var a in doc.XPathSelectElements("//a[starts-with(@href, '___.')]"))
    {
        var href = a.Attribute("href");
        href.Value = href.Value.Replace("___", filename);
    }
    doc.Save(path);
}