views:

43

answers:

3

Suppose there is an element in XML data: <abc:xyz:name attr="value"/>

I'm trying to read it with XmlReader. The problem is that I get XmlException that says

The ‘:’ character, hexadecimal value 0x3A, cannot be included in a name

I have already declared "abc" namespace. I have also tried adding "abc:xyz" and "xyz" namespaces. But this doesn't help at all. I could replace some text before parsing but there may be some more elegant solution. So what should I do?

Here is my code:

XmlReaderSettings settings = new XmlReaderSettings()
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("abc", "");
nsmgr.AddNamespace("xyz", "");
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

// So this reader can't read <abc:xyz:name attr="value"/>
XmlReader reader = XmlReader.Create(path, settings, context);“
+3  A: 

XML entities belong to a single namespace, and there is no nesting of namespace prefixes.

The XmlReader is complaining because it sees a colon, and interprets everything that came before as a prefix. Then it sees another colon, and barfs, because element names are not allowed to have colons in them.

also - just for clarity - when you call nsmgr.AddNamespace(), the "abc" and "xyz" values you passed are prefixes, not namespaces. The namespace is the 2nd arg to that method. In both cases the namespace is the empty string. Not sure why you would do that, though.

You need to step back and figure out why you think you need two namespaces on an element. And also why you're using prefixes for the empty namespace.

Cheeso
Actually, this is just not my xml. I don't need all these namespace for parsing. So I found a workaround with these dummy prefixes. Thanks a lot! :)
Entrase
+4  A: 

You can only really expect the Xml parser to parse correct Xml. The "Xml" in this case is not correctly formed and is in effect not xml.

Your only choice therefore is to do some text munging of the "Xml" so that it becomes valid Xml that can be parsed.

Better yet, if you can, fix whatever thinks its generating Xml so the at really is generating correct Xml.

AnthonyWJones
Unfortunately, I can't fix that. So I have to use text replacing. Thank you! ;)
Entrase
@Entrase: keep in mind that if you don't push back on those who created this invalid XML, they'll only send more invalid XML to more people who will then have to work around the fact that your source doesn't understand XML. We'll get more and more people who send garbage XML thinking it's valid because they saw someone else do it that way. How many different versions of the XML standards should we have to support?
John Saunders
You bet. But it is not possible in this case. These are Winamp Freeform skins :) Probably Nullsoft guys were high while taking such decisions. My current goal is to learn WPF with some practical work. So I'm trying to make my own freeform skin engine. Some important things are already done: bytecode script interpreter and parsing of shitty XMLs.
Entrase
+1  A: 

An element can only belong to one namespace, and a namespaces are defined as (basically) a string, often a URL. With this in mind, it makes little sense to nest namespaces.

You can nest elements, but each element belongs to just one namespace, either the default namespace (no prefix) or the namespace associated with the qualified name prefix.

Another way of thinking about namespaces is to answer the question "who decides the meaning of this element?" Once you know that, you designate a namespace to represent the "who".

Namespaces are there to disambiguate meaning of a node, such as when you have two elements from different sources that are combined together in the xml file. If you were merging a pie chart data on cooking recipe stats and the cooking recipes themselves, then an element "pie" might mean two different things - either a pie chart or a pie recipe. (Contrived example, admittedly!) To ensure each element gets the correct handling (rendering the chart, and consuming the edible pie), the xml should prefix each pie element with a namespace prefix to show what it represents and how it is handled.

In your case, you are using the empty namespace in both instnaces (which is the default), so you can simply leave out the namespace prefix.

mdma