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, nsmg开发者_开发问答r, null, XmlSpace.None);
// So this reader can't read <abc:xyz:name attr="value"/>
XmlReader reader = XmlReader.Create(path, settings, context);“
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.
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.
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.
精彩评论