0

i have developed a project in using c#, reads xml file and redirects the specified output in CSV file.

i am getting error in my code as from XDoc.LoadXml(filenamestr) it goes directly to catch block skipping the in between commands.

try
{
     XmlDocument XDoc = new XmlDocument();
     XDoc.LoadXml(filenamestr);
     XmlDocumentType XDType = XDoc.DocumentType;
     XDoc.RemoveChild(XDType);
     XDoc.Save(filenamestr + ".xml");
}
catch (Exception ex)
{
     Console.WriteLine(ex.Message);
}

and error is

{"Data at the root level is invalid. Line 1, position 1."}

and the xml lines contains:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="MeasDataCollection.xsl"?>
<!DOCTYPE mdc SYSTEM "MeasDataCollection.dtd">
<mdc xmlns:HTML="http://www.w3.org/TR/REC-xml">
<mfh>

please help

Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
Prayag
  • 211
  • 1
  • 13
  • What is this filenamestr ? Is it a file contains your xml data? – Chandan Kumar Dec 11 '17 at 06:55
  • Follow the below links and get idea how to solve your problem by own. https://stackoverflow.com/questions/5748668/data-at-the-root-level-is-invalid?noredirect=1&lq=1 and https://stackoverflow.com/questions/17795167/xml-loaddata-data-at-the-root-level-is-invalid-line-1-position-1 – Chandan Kumar Dec 11 '17 at 07:02

1 Answers1

2

You should change it

XDoc.LoadXml(filenamestr);

to

XDoc.Load(filenamestr);

LoadXml requires xml string instead of xml filename. You should Load method to load xml from file.

lucky
  • 12,734
  • 4
  • 24
  • 46