Illegal Charaters in path

D

DBC User

Hi,

I have a small XML file, I uploaded to a web page. I have the following
code to convert the content I downloaded from web to xml and is giving
"Illegal characters in path", but when I try to see the value in XML
viewer (through add watch) and is showing the content correctly.

Here is the code
...
string b = wc.DownloadString(myurl);
XmlDocument doc = new XmlDocument();
doc.Load(str);
.....

Could someone tell me what is wrong?
When I change the Load to LoadXml I get 'Data at the root level is
invalid. Line 1, position 1"

Here is the XML
<?xml version="1.0" encoding="utf-8" ?>
<Files>
<Application key="one">
<Version>1.0</Version>
</Application>
</Files>
 
C

Cor Ligthert [MVP]

Hi,

You are sure that you start your strings with @?

(we don't see anything from your string that is not accepted)

Cor
 
J

Jon Skeet [C# MVP]

DBC User said:
I have a small XML file, I uploaded to a web page. I have the following
code to convert the content I downloaded from web to xml and is giving
"Illegal characters in path", but when I try to see the value in XML
viewer (through add watch) and is showing the content correctly.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
D

DBC User

Hi Jon,

As I explained before. I have a simple XML
(http://tamil.taisukina.com/ServerReferenceTest.xml) which has only
one. I am able to view the xml on the web page. But when I use the
following code, I am getting "Illegal charaters in path" message on
doc.Load, I changed the Load to LoadXml then I get bad root message.
Here is the sample code where I can make the code fail.


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.Collections;

namespace xmlreadproblem
{
class Program
{
static void Main(string[] args)
{
try
{
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string b =
wc.DownloadString("http://tamil.taisukina.com/ServerReferenceTest.xml");
wc.Dispose();
XmlDocument doc = new XmlDocument();
doc.Load(b);
}
catch (Exception e1)
{
string t = e1.ToString();
//TODO: Make exception more specific
}


}
}
}

Thanks for your time.
 
D

DBC User

In the sample code I used the LoadXml first and got Data at the root
level is invalid. Line 1, position 1. Then I tried load, well I
shouldn't since it is for the loading of xml file :) But I still have
the root level in valid message.
So please read doc.Load as doc.LoadXml(b)

Thanks.

DBC said:
Hi Jon,

As I explained before. I have a simple XML
(http://tamil.taisukina.com/ServerReferenceTest.xml) which has only
one. I am able to view the xml on the web page. But when I use the
following code, I am getting "Illegal charaters in path" message on
doc.Load, I changed the Load to LoadXml then I get bad root message.
Here is the sample code where I can make the code fail.


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.Collections;

namespace xmlreadproblem
{
class Program
{
static void Main(string[] args)
{
try
{
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string b =
wc.DownloadString("http://tamil.taisukina.com/ServerReferenceTest.xml");
wc.Dispose();
XmlDocument doc = new XmlDocument();
doc.Load(b);
}
catch (Exception e1)
{
string t = e1.ToString();
//TODO: Make exception more specific
}


}
}
}

Thanks for your time.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
D

DBC User

One more odd thing I noticed is when I debugged the code and checked
the value in text mode, I see a space betweeb < and Files. Even though
the file doesn't have the space nor the the string have the space.
Where is this space coming from?
Thanks.
DBC said:
In the sample code I used the LoadXml first and got Data at the root
level is invalid. Line 1, position 1. Then I tried load, well I
shouldn't since it is for the loading of xml file :) But I still have
the root level in valid message.
So please read doc.Load as doc.LoadXml(b)

Thanks.

DBC said:
Hi Jon,

As I explained before. I have a simple XML
(http://tamil.taisukina.com/ServerReferenceTest.xml) which has only
one. I am able to view the xml on the web page. But when I use the
following code, I am getting "Illegal charaters in path" message on
doc.Load, I changed the Load to LoadXml then I get bad root message.
Here is the sample code where I can make the code fail.


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.Collections;

namespace xmlreadproblem
{
class Program
{
static void Main(string[] args)
{
try
{
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string b =
wc.DownloadString("http://tamil.taisukina.com/ServerReferenceTest.xml");
wc.Dispose();
XmlDocument doc = new XmlDocument();
doc.Load(b);
}
catch (Exception e1)
{
string t = e1.ToString();
//TODO: Make exception more specific
}


}
}
}

Thanks for your time.

I have a small XML file, I uploaded to a web page. I have the following
code to convert the content I downloaded from web to xml and is giving
"Illegal characters in path", but when I try to see the value in XML
viewer (through add watch) and is showing the content correctly.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
D

DBC User

All,

I modified the XML file and added first letter as X before the root.
The modified the code to find X and then copy everything after X and
try loading the XML and it worked. But what is the first character that
is causing the problem? Following code with the change to XML worked.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.Collections;

namespace xmlreadproblem
{
class Program
{
static void Main(string[] args)
{
try
{
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string b =
wc.DownloadString("http://tamil.taisukina.com/ServerReferenceTest.xml");
wc.Dispose();
XmlDocument doc = new XmlDocument();
int start = b.IndexOf('X')+1;
string t = b.Substring(start, b.Length-start);
doc.LoadXml(t);
}
catch (XmlException e1)
{
string t = e1.ToString();
}
catch (Exception e1)
{
string t = e1.ToString();
//TODO: Make exception more specific
}


}
}
}

DBC said:
One more odd thing I noticed is when I debugged the code and checked
the value in text mode, I see a space betweeb < and Files. Even though
the file doesn't have the space nor the the string have the space.
Where is this space coming from?
Thanks.
DBC said:
In the sample code I used the LoadXml first and got Data at the root
level is invalid. Line 1, position 1. Then I tried load, well I
shouldn't since it is for the loading of xml file :) But I still have
the root level in valid message.
So please read doc.Load as doc.LoadXml(b)

Thanks.

DBC said:
Hi Jon,

As I explained before. I have a simple XML
(http://tamil.taisukina.com/ServerReferenceTest.xml) which has only
one. I am able to view the xml on the web page. But when I use the
following code, I am getting "Illegal charaters in path" message on
doc.Load, I changed the Load to LoadXml then I get bad root message.
Here is the sample code where I can make the code fail.


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.Collections;

namespace xmlreadproblem
{
class Program
{
static void Main(string[] args)
{
try
{
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string b =
wc.DownloadString("http://tamil.taisukina.com/ServerReferenceTest.xml");
wc.Dispose();
XmlDocument doc = new XmlDocument();
doc.Load(b);
}
catch (Exception e1)
{
string t = e1.ToString();
//TODO: Make exception more specific
}


}
}
}

Thanks for your time.


Jon wrote:
I have a small XML file, I uploaded to a web page. I have the following
code to convert the content I downloaded from web to xml and is giving
"Illegal characters in path", but when I try to see the value in XML
viewer (through add watch) and is showing the content correctly.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
C

Cor Ligthert [MVP]

DBC user,

Did you look at my reply as well.

I see you use a double // and a single / in your Url path.

In my idea should that give an "Illegal charachter in path"

What was the problem that you had?

Cor

DBC User said:
One more odd thing I noticed is when I debugged the code and checked
the value in text mode, I see a space betweeb < and Files. Even though
the file doesn't have the space nor the the string have the space.
Where is this space coming from?
Thanks.
DBC said:
In the sample code I used the LoadXml first and got Data at the root
level is invalid. Line 1, position 1. Then I tried load, well I
shouldn't since it is for the loading of xml file :) But I still have
the root level in valid message.
So please read doc.Load as doc.LoadXml(b)

Thanks.

DBC said:
Hi Jon,

As I explained before. I have a simple XML
(http://tamil.taisukina.com/ServerReferenceTest.xml) which has only
one. I am able to view the xml on the web page. But when I use the
following code, I am getting "Illegal charaters in path" message on
doc.Load, I changed the Load to LoadXml then I get bad root message.
Here is the sample code where I can make the code fail.


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.Collections;

namespace xmlreadproblem
{
class Program
{
static void Main(string[] args)
{
try
{
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string b =
wc.DownloadString("http://tamil.taisukina.com/ServerReferenceTest.xml");
wc.Dispose();
XmlDocument doc = new XmlDocument();
doc.Load(b);
}
catch (Exception e1)
{
string t = e1.ToString();
//TODO: Make exception more specific
}


}
}
}

Thanks for your time.


Jon wrote:
I have a small XML file, I uploaded to a web page. I have the
following
code to convert the content I downloaded from web to xml and is
giving
"Illegal characters in path", but when I try to see the value in
XML
viewer (through add watch) and is showing the content correctly.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
D

DBC User

The first letter that is coming from the XML is invalid I tried to
pharse the first letter to char and got a 62345 number and character is
'' so I dropped the first character and everything worked fine. But I
am trying to find out how come I got this weired character in the
begining.
Thanks.
DBC user,

Did you look at my reply as well.

I see you use a double // and a single / in your Url path.

In my idea should that give an "Illegal charachter in path"

What was the problem that you had?

Cor

DBC User said:
One more odd thing I noticed is when I debugged the code and checked
the value in text mode, I see a space betweeb < and Files. Even though
the file doesn't have the space nor the the string have the space.
Where is this space coming from?
Thanks.
DBC said:
In the sample code I used the LoadXml first and got Data at the root
level is invalid. Line 1, position 1. Then I tried load, well I
shouldn't since it is for the loading of xml file :) But I still have
the root level in valid message.
So please read doc.Load as doc.LoadXml(b)

Thanks.

DBC User wrote:
Hi Jon,

As I explained before. I have a simple XML
(http://tamil.taisukina.com/ServerReferenceTest.xml) which has only
one. I am able to view the xml on the web page. But when I use the
following code, I am getting "Illegal charaters in path" message on
doc.Load, I changed the Load to LoadXml then I get bad root message.
Here is the sample code where I can make the code fail.


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.Collections;

namespace xmlreadproblem
{
class Program
{
static void Main(string[] args)
{
try
{
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string b =
wc.DownloadString("http://tamil.taisukina.com/ServerReferenceTest.xml");
wc.Dispose();
XmlDocument doc = new XmlDocument();
doc.Load(b);
}
catch (Exception e1)
{
string t = e1.ToString();
//TODO: Make exception more specific
}


}
}
}

Thanks for your time.


Jon wrote:
I have a small XML file, I uploaded to a web page. I have the
following
code to convert the content I downloaded from web to xml and is
giving
"Illegal characters in path", but when I try to see the value in
XML
viewer (through add watch) and is showing the content correctly.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
C

Claes Bergefall

DBC User said:
Hi,

I have a small XML file, I uploaded to a web page. I have the following
code to convert the content I downloaded from web to xml and is giving
"Illegal characters in path", but when I try to see the value in XML
viewer (through add watch) and is showing the content correctly.

Here is the code
..
string b = wc.DownloadString(myurl);
XmlDocument doc = new XmlDocument();
doc.Load(str);
....

Could someone tell me what is wrong?

XmlDocument.Load doesn't have an overlaod that loads XML from a string. The
string overload expects a filename. Put your string in a StringReader and
then load from that:

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

When I change the Load to LoadXml I get 'Data at the root level is
invalid. Line 1, position 1"

Here is the XML
<?xml version="1.0" encoding="utf-8" ?>
<Files>
<Application key="one">
<Version>1.0</Version>
</Application>
</Files>

LoadXml works with the above XML. Not sure why you get an error

/claes
 
D

DBC User

Hi Claes,

I am getting this file from web. I am downloading this file using
webclient and how how it is adding 1 character before the actual file.
Stripping the first character solved the problem though. But why and
what is this first character?

Thanks.
 
M

Marc Gravell

Well, the binary looks like EF BB BF which is the UTF8 BOM; not sure why the
encoder isn't dealing with it, though...

Marc
 
R

Richard Whitcher

DBC,
I am getting this file from web. I am downloading this file using
webclient and how how it is adding 1 character before the actual file.
Stripping the first character solved the problem though. But why and
what is this first character?

You should be able to load the xml document without using the webclient:

....
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("http://www.someurl.com/file.xml");
....

That might solve the issue and with less code! ;o)

Regards,

Richard
 
D

DBC User

Richard,
Your magic worked. It seems when I download the file using webclient
and then process I have the problem but if I load the xml directly from
the web, I do not have the problem.
Thanks again.
 
J

Jon Skeet [C# MVP]

Marc Gravell said:
Well, the binary looks like EF BB BF which is the UTF8 BOM; not sure why the
encoder isn't dealing with it, though...

The encoder is dealing with the BOM and decoding it to U+FEFF
correctly. XmlDocument.LoadXml doesn't appear to like that,
unfortunately. Either way, there's still the rogue 'X' though.
 
J

Jon Skeet [C# MVP]

DBC User said:
As I explained before. I have a simple XML
(http://tamil.taisukina.com/ServerReferenceTest.xml) which has only
one. I am able to view the xml on the web page. But when I use the
following code, I am getting "Illegal charaters in path" message on
doc.Load

That's because it's trying to use the XML data itself as a URL. That's
not the way to go.
I changed the Load to LoadXml then I get bad root message.

Right - that's the correct call to make - the problem is that the file
is essentially corrupt.
Here is the sample code where I can make the code fail.

Thanks.


Okay, let's look at this. Firstly, if you try to load the URL into
Firefox or IE, they both fail in the same way. That's a strong
suggestion that it's not the code that's wrong (when you've changed
Load to LoadXml) but the file.

The first two characters of the file are U+FFEF (which is the byte
order mark) and 'X'. Now, the byte order mark confuses
XmlDocument.LoadXml, which is debatable behaviour, but the presence of
the 'X' is very puzzling. It really just shouldn't be there.

You need to work out why the file is broken in terms of the 'X' to
start with, and then either use the overload given by Richard or
manually strip out the BOM.
 
M

Marc Gravell

The OP added this to allow the manual stripping by finding the first X and
adding one. Easy to miss in the signal : noise ratio of the chain though ;-p

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top