Groups | Blog | Home
all groups > dotnet xml > february 2007 >

dotnet xml : Removing xmlns:xsd and xmlns:xsi when using ImportNode


Keith Patrick
2/22/2007 3:12:10 PM
I'm doing some document merging where I want to bring in an XmlDocument and
import its document element into another document deeper in its tree.
However, when serializing my underlying objects, .Net likes to add these
namespaces:
<RootNode xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ChildNode xmlns="MyObjectHierarchyNamespace/>
</RootNode>

The problem this is causing me is that any node(s) that have those
namespaces get properly imported into the XML, but those nodes fail to
deserialize. Is there a wya to prevent the aforementioned namespace issue
(in either importation or deserialization) without manually removing the
attributes during merging?

John Saunders
2/22/2007 7:36:15 PM
[quoted text, click to view]


Keith, if they fail to deserialize, it's probably because you don't have the
correct namespaces declared on the properties they should deserialize into.
For instance, what namespace do you have declared on the ChildNode property?

In other words, this is more likely because of the
xmlns="MyObjectHierarchyNamespace" rather than because of the xmlns:xsd or
xmlns:xsi namespace nodes.

Here's a very brief lesson on XML namespaces, in case you need it:

<foo xmlns="bar"/>

and

<foo xmlns="baz"/>

Are elements of two totally different and unrelated types. If you had a
property that the first one deserialized into, you would not want the second
one to be able to deserialize into that same property, because it's a
totally different entity that happens to also have the local name "foo".

John

Keith Patrick
2/23/2007 9:39:55 AM
I've got all my classes tagged with XmlType("MyObjectHierarchyNamespace")
but not the properties of those types. But the thing is, my namespace is
already being declared on the primary document so that when it merges in,
I'll have:

<Grades xmlns="MyObjectHierarchyNamespace">
<Grade Name="1st">
<Subjects>
<Subject Name="Math">
<Segments>
<Segment Name="SegmentOne" />
</Segments>
</Subject>
<Subject Name="English">
<Segments>
<Segment Name="SegmentOne">
<Modules>
<Module
xmlns="MyObjectHierarchyNamespace"/>
<Module />
<Module />
</Modules>
.
.
.

In this case, everything shows up except that first module (Modules contains
2 Module objects). public class Module has
XmlType("MyObjectHierarchyNamespace") as does ModuleCollection, but not the
property Modules

Keith Patrick
2/23/2007 9:53:32 AM
Ahh, found a fix. I was using XmlTypeAttribute, but converting to
XmlRootAttribute wound up working.


AddThis Social Bookmark Button