Reputation: 357
I am getting a root element missing exception for this code. The code is trying to read the same xml file again. There are around 150 xml files in this location. Can anybody help please?
foreach (string file in Directory.EnumerateFiles("C:\\Path\\", "*.xml"))
{
string FileName = Path.GetFileNameWithoutExtension(file);
XPathDocument myXPathDoc = new XPathDocument(file);
XslCompiledTransform myXslTrans = new XslCompiledTransform();
/* loading XSLT */
myXslTrans.Load("ABCXSLT.xsl");
/* creating Output Stream */
XmlTextWriter myWriter = new XmlTextWriter(FileName + "_cleaned.xml", null);
/* XML transformation */
myXslTrans.Transform(myXPathDoc, null, myWriter);
myWriter.Close();
}
Upvotes: 0
Views: 1342
Reputation: 11955
Why don't you try this, and find all the files that are missing a root element:
foreach (string file in Directory.EnumerateFiles(path, "*.xml"))
{
try { XElement.Load(file); }
catch
{
string name = Path.GetFileName(file);
Console.WriteLine(name + " is missing a root element");
}
}
If it catches none, then it is your code that is mishandling the xml file.
Upvotes: 0
Reputation: 611
I suspect you'll get the same error, but you may be able to isolate better and determine if its caused by the xslt or xml files.
Move the loading of the xslt outside of the loop:
XslCompiledTransform myXslTrans = new XslCompiledTransform();
/* loading XSLT */
myXslTrans.Load("ABCXSLT.xsl");
Then execute your loop:
foreach (string file in Directory.EnumerateFiles(@"C:\Path\", "*.xml")) {
string FileName = Path.GetFileNameWithoutExtension(file);
XPathDocument myXPathDoc = new XPathDocument(file);
/* creating Output Stream */
XmlTextWriter myWriter = new XmlTextWriter(FileName + "_cleaned.xml", null);
/* XML transformation */
myXslTrans.Transform(myXPathDoc, null, myWriter);
myWriter.Close();
}
Upvotes: 1