XmlException - The ':' character, hexadecimal value 0x3A, cannot beincluded in a name

  • Thread starter Thread starter simon.vriesema
  • Start date Start date
S

simon.vriesema

Hi Guys,

Ive seen this posted around on the interwibble a few times, but no
post so far has helped me to understand the problem any better

i appreciate the error is because of "well-formed" xml practices


what i am trying to do however is read an "xml" file using the
following code snippet:


//---------------------------------------------------------------------------------------------------------------------------
XDocument xmldoc =
XDocument.Load(openFileDialog1.FileName);
var query = from c in
xmldoc.Descendants("gml:coordinates")
select c;

foreach (var q in query)
{...

//---------------------------------------------------------------------------------------------------------------------------

im actually reading in a standard GML document which uses the
extensible markup language.

im searching for the element of "gml:coordinates" to pull the contents

why is it throwing the following exception:
The ':' character, hexadecimal value 0x3A, cannot be included in a
name.

how can i get around this, i was going to use the LINQ capabilities in
2008 to enumerate through the xml document, i could do this a long way
around, but id prefer not to.
 
why is it throwing the following exception:
The ':' character, hexadecimal value 0x3A, cannot be included in a
name.

It's trying to treat it all as just the tag name, with no namespace.
how can i get around this, i was going to use the LINQ capabilities in
2008 to enumerate through the xml document, i could do this a long way
around, but id prefer not to.

Separate out the namespace from the name:

XNamespace gml = "gml";

foreach (var q in xmldoc.Descendants(gml+"coordinates"))

(Note that unless you're doing something else in your real query
expression, there's not a lot of point in using it instead of just
calling Descendants. There's a subtle difference in terms of an extra
call to Select, but it's almost certainly irrelevant to you.)
 
Thanks Jon

It doenst however seem to work

im still learning c#, its not too much different than bc++.



So anyway, what youve suggested makes sense, however it doesnt seem to
find any elements

for now all im trying to do is write the decendants to a richtextbox

here is an extract of a gml file:

<gml:polygonMember>
<gml:Polygon>
<gml:outerBoundaryIs>
<gml:LinearRing>
<gml:coordinates>142.27996826171875,-10.265556335449219
142.21052551269531,-10.236804008483887
142.20135498046875,-10.225555419921875
142.18942260742187,-10.204166412353516
142.212890625,-10.15694522857666
142.22857666015625,-10.145556449890137
142.28414916992187,-10.135695457458496
142.31692504882812,-10.151666641235352
142.33053588867187,-10.171945571899414
142.3399658203125,-10.191389083862305
142.33856201171875,-10.202777862548828
142.28886413574219,-10.261112213134766
142.27996826171875,-10.265556335449219 </gml:coordinates>
</gml:LinearRing>
</gml:outerBoundaryIs>
</gml:Polygon>
</gml:polygonMember>






my current code:

var query = from c in xml2.Descendants(gml +
"coordinates")
select c;

foreach (var q in query)
{
richTextBox1.Text = q.ToString();
}


all it will do is display the last "captured" element into the
richtextbox, im not needing to do anything fancy yet

but essentially it will read through each "q" and extract the contents
of the gml:coordinates

if you can suggest a different way to do this, id be quite grateful

btw, im finding this linq thing to be quite powerful, its quite a
change from using borland c++
 
im still learning c#, its not too much different than bc++.

That seems unlikely - by the time you include the C# 2 and 3 features
:)
So anyway, what youve suggested makes sense, however it doesnt seem to
find any elements

That's 'cos I mucked up the example, I'm afraid. You need to specify
the namespace as it's declared in the file, not the sort of alias for
it.

Try with:

XNamespace gml = "http://www.opengis.net/gml";

instead. (I've tried it with your sample and it works fine for me.)
btw, im finding this linq thing to be quite powerful, its quite a
change from using borland c++

Yes, LINQ is incredibly powerful.
 
That seems unlikely - by the time you include the C# 2 and 3 features
:)


I meant in terms of syntax, i come from a vb.net background turned c++
developer

i was expecting something more different in syntax than c++, but it
seems generally the same


as for the xml problem, seems to work nicely now, thankyou

curious though, why does it insert the xml namespace into the element

<gml:coordinates xmlns:gml="http://www.opengis.net/gml">

further to that, if your feeling up to it, could u explain to me how
the code that youve suggested actually works?
 
I meant in terms of syntax, i come from a vb.net background turned c++
developer

Have you seen LINQ yet? That's certainly not C++-like :)
i was expecting something more different in syntax than c++, but it
seems generally the same

It's certainly "curly braces and semi-colons".
as for the xml problem, seems to work nicely now, thankyou

curious though, why does it insert the xml namespace into the element

<gml:coordinates xmlns:gml="http://www.opengis.net/gml">

Well, that *is* the namespace.
further to that, if your feeling up to it, could u explain to me how
the code that youve suggested actually works?

I won't pretend to be an expert, but basically my understanding is that
"gml" is just a short name for "http://opengis.net/gml" effectively.
 
Put your namespace inside of curly brackets.

string ns = "{" + xd.Root.Name.NamespaceName + "}";

IEnumerable<XElement> d = xd.Descendants(ns + "People").Descendants();
 
Back
Top