Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XmlDocument: The data at the root level is invalid. 2

Status
Not open for further replies.

Tokhra

Programmer
Oct 1, 2003
134
ES
Hi all,

I have this code:

XmlDocument doc = new XmlDocument();
doc.LoadXml(path);

(path is c:\InetPub\Bleh\Config.xml)

The xml document contains:

<?xml version="1.0" encoding="utf-8" ?>

<urlrewriter>
<rule pattern="bleh" rewrite="blah" />
</urlrewriter>


And when it runs I get:

The data at the root level is invalid. Line 1, position 1.

If I read the xml doc using a XmlTextReader it works fine, I only get this error with the XmlDocument.

Im all out of ideas on this one so any suggestions would be much appreciated,

Thanks,
Matt.


 
The root element must be on the line following the XML declaration. Get rid of the blank line and try again.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
OK so now I have:

<?xml version="1.0" encoding="utf-8"?>
<urlrewriter>
<rule pattern="bleh" rewrite="blah" />
</urlrewriter>

No white space after the first tag either, still getting the same thing :(

Any other ideas?
 
How many "<urlrewriter>" tags do you have in your document? There can be only one element at the root level.

The only other thing I can suggest is that you capitalize "UTF" in your XML tag, but that shouldn't really matter.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
What was in my last post is the complete xml file.

Just out of curiosity I tried to pointed the XmlDocument to Web.config and it does the same with that..

I wonder if XmlDocument's constructor takes a path or a string with the actual xml? the documentation isn't too clear on that.

Basically, the goal is to loop the <rule> elements, so id GetElementByName("urlwriter"); then loop through the node list.

Any other alternative or even cleaner ways of doing this?

Thanks,
Matt.
 
it's because with XmlDocument.Load it tries to load from the string you pass as argument, not from the filepath pointed by the string. use XmlTextReader and then XmlDocument.Load from the text reader.
 
Ah, I'll give that a go later on.

So its:

XmlTextReader reader = new XmlTextReader(path);

XmlDocument doc = new XmlDocument();
doc.LoadXml(reader);

or

doc.LoadXml(reader.Read());

?

Thanks,
Matt.

 
the first one.
Code:
    XmlDocument doc = new XmlDocument();
    XmlTextReader reader = new XmlTextReader(path);
    reader.WhitespaceHandling = WhitespaceHandling.None;
    reader.MoveToContent();
    reader.Read();
    doc.Load(reader);
 
So given:

<?xml version="1.0" encoding="utf-8"?>
<urlrewriter>
<rule pattern="bleh" rewrite="blah" />
</urlrewriter>

how could I loop the <rule> elements? I have just written this code:

Code:
XmlTextReader reader = new XmlTextReader(m_configPath);
				reader.WhitespaceHandling = WhitespaceHandling.None;
				reader.MoveToContent();
				reader.Read();

			XmlDocument doc = new XmlDocument();
				doc.Load(reader);

			XmlNodeList rules = doc.GetElementsByTagName("urlrewriter");

			if (rules.Count == 0)
			{
				context.Response.Write("No Nodes Found!");
			}
			else
			{
				context.Response.Write(String.Format("{0} Nodes Found!", rules.Count.ToString()));
			}

			foreach(XmlNode node in rules)
			{
				string pattern = node.Attributes["pattern"].Value;
				string rewrite = node.Attributes["rewrite"].Value;

				context.Response.Write(String.Format("Pattern: {0}, Rewrite: {1}", pattern, rewrite));
			}

And the only response I ever get is "No Nodes Found", Any ideas?

Sorry im a bit of a noob using the .net xml namespace :|

Thanks,
Matt.
 
remove
Code:
reader.MoveToContent();

because this already moves the reader to the content of the first node.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top