XML Deserialization - xmlns not expected 
March 18th, 2009, 05:45 PM
| | Newbie | | Join Date: Mar 2009 Location: IL
Posts: 6
| |
I'm using XmlSerializer in C# to deserialize an xml into classes. First I generated the code from the schema using xsd.exe The error I recieve is:
Exception:There is an error in XML document (2, 2).
Inner Exception: <applications xmlns='urn:xmlns:COMMONCENSUS:CommonFormat:IMSchem a'> was not expected.
I've read numerous threads and realize it is a namespace issue, but none seem to help with this particular problem. I've included the xml, and the Schema file below as well as the code. Thanks in advance. xml: - <?xml version="1.0" encoding="utf-8" standalone="yes"?>
-
<applications xmlns="urn:xmlns:COMMONCENSUS:CommonFormat:IMSchem a" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exportType="T">
-
<application guid="{9A44D2FF-90EB-45BE-BC88-CF2107B2CA4C}" xmlns="urn:xmlns:COMMONCENSUS:CommonFormat:IMSchem a">
-
-----
-
</application>
-
</applications>
xsd: - <?xml version="1.0" encoding="utf-8"?>
-
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:xmlns:COMMONCENSUS:CommonFormat:IMSchem a" xmlns:NS="urn:xmlns:COMMONCENSUS:CommonFormat:IMSc hema" targetNamespace="urn:xmlns:COMMONCENSUS:CommonForm at:IMSchema" elementFormDefault="qualified">
-
<xs:element name="applications">
-
<xs:complexType>
-
<xs:sequence>
-
<xs:element name="application" type="ApplicationType" minOccurs="0" maxOccurs="unbounded">
-
<xs:key name="personKey">
-
<xs:selector xpath="persons/person" />
-
<xs:field xpath="id" />
-
</xs:key>
-
<xs:keyref name="applicantKeyRef" refer="personKey">
-
<xs:selector xpath="applicantID" />
-
<xs:field xpath="." />
-
</xs:keyref>
-
<xs:keyref name="insuredKeyRef" refer="personKey">
-
<xs:selector xpath="coverages/coverage/insuredID" />
-
<xs:field xpath="." />
-
</xs:keyref>
-
</xs:element>
-
</xs:sequence>
-
<xs:attribute name="exportType" type="ExportType" />
-
</xs:complexType>
-
</xs:element>
-
-----
-
</xs:schema>
Code: - // Read the xml file
-
TextReader reader = new StreamReader("C:\\CommonSensusXml\\Policy_87654321 _210_Dion-Paul_WSA.xml");
-
-
//Application Type
-
XmlSerializer serializer = new XmlSerializer(typeof(ApplicationType));
-
ApplicationType appType = (ApplicationType)serializer.Deserialize(reader);
Last edited by Dormilich; March 19th, 2009 at 06:20 AM.
Reason: added [code] tags
| 
March 19th, 2009, 08:04 PM
| | Moderator | | Join Date: Mar 2006
Posts: 1,103
| | | re: XML Deserialization - xmlns not expected
Your namespace URI "urn:xmlns:COMMONCENSUS:CommonFormat:IMSchem a"
is not allowed to have spaces.
Beyond that, I would recommend explicitly assigning the schema, eg like:
xsi:schemaLocation = "urn:xmlns:COMMONCENSUS:CommonFormat:IMSchema SchemaFile.xsd"
| 
March 19th, 2009, 08:10 PM
| | Newbie | | Join Date: Mar 2009 Location: IL
Posts: 6
| | | re: XML Deserialization - xmlns not expected
Thanks for the reply jkmyoung. Unfortunately, that is a typo and there is no space in the namespace uri. It should read like this: urn:xmlns:COMMONCENSUS:CommonFormat:IMSchema
Any other thoughts?
Thanks...
| 
March 19th, 2009, 08:31 PM
| | Moderator | | Join Date: Mar 2006
Posts: 1,103
| | | re: XML Deserialization - xmlns not expected
Assuming your targetNamespace should also read: targetNamespace="urn:xmlns:COMMONCENSUS:CommonForm at:IMSchema"
See edit to above comment as well.
| 
March 19th, 2009, 09:12 PM
| | Newbie | | Join Date: Mar 2009 Location: IL
Posts: 6
| | | re: XML Deserialization - xmlns not expected
jkmyoung,
Yes, the target namespace is correct. I tried to explicitly assign the schema like you mentioned but with the same error message as listed in my first post.
To me, the error reads like there is a namespace specified in the xml file where it does not exist in the schema file. As far as I can tell, that's not the case.
Thanks again for your prompt reply...any further advice is much appreciated.
| 
March 20th, 2009, 04:51 PM
| | Moderator | | Join Date: Mar 2006
Posts: 1,103
| | | re: XML Deserialization - xmlns not expected
Just to clarify, targetnamespace should be
"urn:xmlns:COMMONCENSUS:CommonFormat:IMSchema"
not what was posted above.
Accidentally copied/paste, and forgot to replace the offending text.
| 
March 23rd, 2009, 05:53 PM
| | Newbie | | Join Date: Mar 2009 Location: IL
Posts: 6
| | | re: XML Deserialization - xmlns not expected
Thanks again but still the same error. I've noticed that when I copy and paste from the xml into this text editor, it will add spaces in several places.
I am still unable to find a solution for this. Could it be something in the way the schema is structured? The element name 'applications' acts as a parent element in the xml file but in the schema, there are several other elements that stand alone outside of the 'applications' element. If I need to provide more info I can create a mock xml and remove any sensitive data.
| 
April 21st, 2009, 02:52 PM
| | Newbie | | Join Date: Apr 2009
Posts: 2
| | | re: XML Deserialization - xmlns not expected
Hey, I'm having a very similar problem. Don't know if you got yours fixed but if I find mine, I will post it here.
My hunch is that it has to do with the declaration of the XmlSerializer. There is a constructor that does as follows (from http://msdn.microsoft.com/en-us/libr...(VS.71).aspx):
"Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and vice versa. Specifies the default namespace for all the XML elements."
.
.
[C#] public XmlSerializer(Type, string);"
-Matt
| 
April 21st, 2009, 07:43 PM
| | Newbie | | Join Date: Apr 2009
Posts: 2
| | | re: XML Deserialization - xmlns not expected
Ok, here's the fix for me. I was getting the same (2,2) and "was not expected" set of errors.
The section of my XML that was a problem looks like this: - <ns2:Info xmlns:ns2="http://www.something.org/whatsit-0-3-4">
This is the significant part of the code that xsd.exe generated for me:
ORIGINAL: - [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.something.org/whatsit-0-3-4")]
-
[System.Xml.Serialization.XmlRootAttribute("Some_Namespace", Namespace = "http://www.different.org/blah-1-3-2", IsNullable = false)]
FIXED & WORKING: - [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.something.org/whatsit-0-3-4")]
-
[System.Xml.Serialization.XmlRootAttribute("Info", Namespace = "http://www.something.org/whatsit-0-3-4", IsNullable = false)]
The problem for me was that the namespace was the root attribute in the feed I was getting, but the root was listed as a different namespace! Once I changed that and recompiled, it was able to deserialize, because it could now understand what it was looking at.
Hope this helps!
Last edited by Dormilich; April 22nd, 2009 at 07:34 AM.
Reason: added [code] tags
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|