- Home /
Search function through multiple XML files
I'm working on a keyword search function throughout +/- 80 .xml-files, based on this post. My code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.Linq;
using System.Xml.Linq;
using System.IO;
public static class SearchFunctionNew
{
public static List<XDocument> FindDocumentsByHeader(string comparisonText)
{
List<XDocument> docsToReturn = new List<XDocument>();
List<string> filePaths = Directory.GetFiles("Assets/Resources/Infopoints").ToList();
foreach(string path in filePaths)
{
XDocument docToCheck = XDocument.Load(path);
if (docToCheck.Element("infopoint").Value.Contains(comparisonText.ToUpper()))
{
docsToReturn.Add(docToCheck);
}
}
return docsToReturn;
}
}
However, when I run this function with a string inserted as keyword, I'm getting the following error on line 17:
XmlException: Text node cannot appear in this state. *PathToUnityProject*/Assets/Resources/Infopoints/Infopunt1.xml.meta Line 1, position 1.
Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace)
After a lot of searching and trial & error, I figure it has something to do with the specific syntax for XDocument and XmlDocument. But I can't seem to figure out how to get it working correctly. Any help or directions are very much appreciated! :)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612143540im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how do I search for an atribute in a XML file? 0 Answers
Searching XML document 2 Answers
A node in a childnode? 1 Answer
Search value in XML 2 Answers
Reading level specific file in build (from another level) 0 Answers