I have a set of schemas for two different XML files that share common
structures through the use of "import".
Eventually, the main XSD file (which just gathers pieces from the
common stuff to show what goes in this root element) contains:
<xs:element name= "ImageBox" type="ns:ImageBox_t" form="qualified"
/>
I use a validating reader to pull this into an XmlDocument. Then, step
through the immediate children of the DocumentElement and then create
individual objects based on what I found. To do that,
XmlNodeReader nodereader= new XmlNodeReader (el);
XmlSerializer s= blah blah blah
ImageBox_t newbie= (ImageBox_t) s.Deserialize (nodereader);
// >> do something with newbie.
That worked fine in a different XML file, where that shcema contains:
<xs:element name="ImageBox" type="MIM:ImageBox_t" />
The prefix is different, but both XSD files have the same
targetNamespace which is the same namespace as the targetNamespace in
the XSD file where ImageBox_t is defined. Note that there is only one
place where ImageBox_t is defined, and I am not trying to read
different things with one serializer as is so often the answer in these
posts.
Now, when deserializing from the file that contains
<clinical:Imagebox>... , where clinical is the prefix defined for that
namespace in that XML file, I got an error deserializing to the effect
of "<ImageBox xmlns='blahblah'> was not expected.
I added an [XmlRoot] C# attribute to the ImageBox class definition
telling it the Namespace to expect. Now it deserializes it without
complaint.
BUT, the object is empty. It did not object to the opening tag's name,
but it ignored everything inside it without complaint, and left the
object with [XmlAttribute] things set and any [XmlElement]'s left where
the default constructor had it.
*** >>> I'm at my wit's end here. How can I get the deserializer to
read from this initial node that has a qualified name?
Meanwhile, now it did not read the first one anymore, with a similar
but opposite error. I could not change the XmlNode I started from
because (1) the NamespaceURI is read-only and (2) there is no public
constructor to make a new one copying over all the children, and (3)
changing the Prefix didn't do anything. So I used
XmlAttributeOverrides to change the XmlRoot Namespace back to empty,
and use two different XmlSerializer's based on whether a namespace is
present or not in that node:
XmlSerializer s= (el.NamespaceURI == string.Empty) ?
my_serializer_NC : my_serializer;
That part works, but is a work-around. I would like to know how to get
the serializer to properly recognise that the element is indeed in the
same target namespace.
But my show-stopping problem, which I must do something about quickly,
is getting the prefixed one to be read at all. It's just not working,
no work-around in sight!
Please help.
--John