473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

XML Deserialization - xmlns not expected

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.

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8" standalone="yes"?>
  2. <applications xmlns="urn:xmlns:COMMONCENSUS:CommonFormat:IMSchem a" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exportType="T">
  3. <application guid="{9A44D2FF-90EB-45BE-BC88-CF2107B2CA4C}" xmlns="urn:xmlns:COMMONCENSUS:CommonFormat:IMSchem a">
  4. -----
  5. </application>
  6. </applications>
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <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">
  3. <xs:element name="applications">
  4. <xs:complexType>
  5. <xs:sequence>
  6. <xs:element name="application" type="ApplicationType" minOccurs="0" maxOccurs="unbounded">
  7. <xs:key name="personKey">
  8. <xs:selector xpath="persons/person" />
  9. <xs:field xpath="id" />
  10. </xs:key>
  11. <xs:keyref name="applicantKeyRef" refer="personKey">
  12. <xs:selector xpath="applicantID" />
  13. <xs:field xpath="." />
  14. </xs:keyref>
  15. <xs:keyref name="insuredKeyRef" refer="personKey">
  16. <xs:selector xpath="coverages/coverage/insuredID" />
  17. <xs:field xpath="." />
  18. </xs:keyref>
  19. </xs:element>
  20. </xs:sequence>
  21. <xs:attribute name="exportType" type="ExportType" />
  22. </xs:complexType>
  23. </xs:element>
  24. -----
  25. </xs:schema>
Expand|Select|Wrap|Line Numbers
  1. // Read the xml file
  2. TextReader reader = new StreamReader("C:\\CommonSensusXml\\Policy_87654321 _210_Dion-Paul_WSA.xml");
  4. //Application Type
  5. XmlSerializer serializer = new XmlSerializer(typeof(ApplicationType));
  6. ApplicationType appType = (ApplicationType)serializer.Deserialize(reader);
Mar 18 '09 #1
8 30720
2,057 Expert 2GB
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"
Mar 19 '09 #2
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?

Mar 19 '09 #3
2,057 Expert 2GB
Assuming your targetNamespace should also read: targetNamespace="urn:xmlns:COMMONCENSUS:CommonForm at:IMSchema"

See edit to above comment as well.
Mar 19 '09 #4
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.
Mar 19 '09 #5
2,057 Expert 2GB
Just to clarify, targetnamespace should be
not what was posted above.

Accidentally copied/paste, and forgot to replace the offending text.
Mar 20 '09 #6
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.
Mar 23 '09 #7
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);"

Apr 21 '09 #8
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:

Expand|Select|Wrap|Line Numbers
  1. <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:
Expand|Select|Wrap|Line Numbers
  1. [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.something.org/whatsit-0-3-4")]
  2. [System.Xml.Serialization.XmlRootAttribute("Some_Namespace", Namespace = "http://www.different.org/blah-1-3-2", IsNullable = false)]
Expand|Select|Wrap|Line Numbers
  1. [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.something.org/whatsit-0-3-4")]
  2. [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!
Apr 21 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Linus | last post by:
Hi, I'm having problems with some very simple deserialization code and would appreciate it very much if I could get some help here. The following is the code:...
by: Mike Sarbu | last post by:
Hello all, I have an XML file like this: <?xml version="1.0" encoding="utf-8"?> <SomeObject xmlns="http://www.abcinc.com/objectdefinition"...
by: amrendrakr | last post by:
Hi, I am getting error "<DECISION_REQIEST xmlns=''> was not expected. excepiton: System.InvalidOperationException any clue: why getting error: input: <DECISION_REQUEST...
by: parrot toes | last post by:
Summary: I have been trying to make requests of a web service provided by Axis using a dotnet client with code generated by wsdl.exe and have been getting exceptions when trying to process the...
by: PeterW | last post by:
I have an xml file containing some stuff. I use xsd to generate a schema and again to create classes from the schemas. added using System, System.IO; added to all classes added namespace ROC...
by: John Glover | last post by:
To whoever can help, I've been having a problem with XML deserialization lately. I needed to serialize and deserialze two objects which inherited from Hashtable. Because IDictionary...
by: John Smith | last post by:
This is what I am trying to do: FileStream fs = new FileStream("C:\\request1.xml", FileMode.Open); XmlSerializer x = new XmlSerializer(typeof(PodpisaniDokumentTip)); PodpisaniDokumentTip doc =...
by: snowie | last post by:
I have a simillar problem to what others have had before me. I just can't find the solution in any earlier post here at the scripts (or any other forum). I am calling a web service that returns a...
by: nwbabcock | last post by:
I'm using XmlSerializer to deserialize an xml into classes. The error I recieve is: Exception:There is an error in XML document (2, 2). Inner Exception: <applications...
by: Stubbie | last post by:
have an object (InputFile) that I am able to successfully serialize to XML and deserialize back into the object through an IXmlSerializable interface. Now I'm trying to serialize a List<InputFile>...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.