0

I am receiving an error regarding improper format when loading an xml document in c#. When using the following code,

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(fileName);

I receive the error afterwards:

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

However, if I change the first occurrence to the following, everything works as expected and the type of xmlDoc is in fact XmlDocument:

var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(fileName);

A snippet of my xml is file is below:

<?xml version="1.0" encoding="utf-8"?>
<AutomatedTests type="asdf">
    <TestGroup>
    </TestGroup>
</AutomatedTests>

Is there any explanation as to why this could be happening?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Polosky
  • 88
  • 5
  • 13
  • Is your xml really missing a closing ">", or did you just mis-copy?:) – DVarga Jul 15 '16 at 16:26
  • Ah good catch. I miscopied! – Polosky Jul 15 '16 at 16:26
  • 1
    [XmlDocument.LoadXml Method (String)](https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml(v=vs.110).aspx) - Loads the XML document from the specified string. while [XmlDocument.Load Method](https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.load(v=vs.110).aspx) - Loads the specified XML data from a Stream, a URL, a TextReader, or an XmlReader. – Nkosi Jul 15 '16 at 17:13
  • @StuartLC this isn't a duplicate question. The question that you linked refers to XmlDocument.Load, not XmlDocument.LoadXml, they're two separate unique functions. Additionally, this was just a typo. If you're going to close it, close it for not being a good question. – Polosky Feb 11 '20 at 18:00
  • @Hondros looks like a Dupe. OP on the linked question tried to do `Doc.LoadXml(@"C:\MappingFiles\InputFile.xml");`, and you're trying to do `xmlDoc.LoadXml(fileName);`? The fix in both instances is simple - use `Load` for files, streams etc, and `LoadXml` is to be used only if you already have the xml in a string. – StuartLC Feb 12 '20 at 10:41

2 Answers2

2

Assuming filename is the path, try the below code.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);

XmlDocument.Load loads XML either from a stream, TextReader, path/URL, or XmlReader and XmlDocument.LoadXml loads the XML contained within a string.

Nagaraj Raveendran
  • 1,150
  • 15
  • 23
  • This is the exact code that I have in the first snippet above. It returns the error "Data at the root level is invalid. Line 1, position 1." – Polosky Jul 15 '16 at 17:08
  • 2
    @Hondros you have `LoadXml` in both snippets – Nkosi Jul 15 '16 at 17:09
  • I can't believe I missed that. Coincidentally, that's the same call that I use in a different function a few lines above that in my code. Thanks for that catch, everything is running properly now! – Polosky Jul 15 '16 at 17:13
-1

If you really want to read it in as a string and don't want to do XMLDocument.Load() as another suggested, you can do XMLDocument.LoadXml(), but it should be as a string, first:

string myFilePath = @"C:\MappingFiles\InputFile.xml";
string allText = File.ReadAllText(myFilePath);

XmlDocument xmlDoc = new XmlDocument();
try
{
    xmlDoc.LoadXml(allText);
}
catch (Exception ex)
{
    Console.WriteLine("Error: " + ex.Message);
}

I found that this can work even when .Load() on the file, itself, does not.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Dear Moderator Who Deleted My Answer Because It Was Answered On Another Question The Same Way:

Don't just delete people's answers - if it's the same answer, it's because IT'S THE SAME QUESTION. Close the QUESTION, don't delete answers.

vapcguy
  • 7,097
  • 1
  • 56
  • 52