XmlSchemaProvider and the Duplicate Namespace problem SOLVED

January 15, 2007 on 6:11 pm | In .NET Coding |

Ok. I’ve hated, despised, abhored, and feared the XML schema provider and using the XML SOM to generate schema because the thing makes you use separate namespaces for EVERY single thing… right?

WRONG!

There is a tiny little trick, that just about EVERYONE misses that lets you get away with MURDER. Even though in this case it should technically be legal.

In the Schema provider static method, you have to initialize the XmlResolver of the schemaset like this:


public static XmlQualifiedName SchemaProviderMethod(XmlSchemaSet xs)
{

            XmlSchema mySchema = <get your schema somehow kids>...
 
            // LOOK! No Collisions! Cause I get new resolver each time!
            xs.XmlResolver = new XmlUrlResolver(); // <--- look how easy!
            xs.Add(mySchema);
 
            return new XmlQualifiedName("MyComplexType", "namespacehere");
}
 

I now feel like it does what it’s supposed to do.. but jeeeeeeeez, it shouldn’t be so hard to find this information.

Edit: I did see this on MSDN’s example code… but the entire explanation therein failed to mention the monstrous importance of the fact that you MUST initialize that resolver if you are going to be able to survive this venture. I for one don’t automatically copy and paste code unless I see some value in it at the time, so when I read that about a year or more ago, I didn’t see it explained, and I didn’t realize why I was in such a hole until… today at 3:30pm. I looked over the whole thing again, for the 20 millionth time, and this time I saw that stupid line… none of the articles I’ve seen anywhere (and thus my own on DevX) included this little tidbit, so I feel this burning desire to highlight it. Excuse me for a bit while I get very angry at nobody in particular for no good reason at all. Things like that just frustrate a person. I won’t blame microsoft like you’d expect me to. So there.

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds. Valid XHTML and CSS. ^Top^