473,574 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple namespace deserialize problem

Hello

I want to deserialize the following xml

<anyRoot xmlns:xsd="http ://www.w3.org/2001/XMLSchema" xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance" xmlns="http://ogsa.globus.org/any-service"><QName >abc</QName></anyRoot

using

TextReader tr = new StreamReader("c :\\test.xml")
anyRootType rootType = (anyRootType)se rializer.Deseri alize(tr)
tr.Close()

but always get the exception

Message: There is an error in XML document (1, 2)
Message: <anyRoot xmlns='http://ogsa.globus.org/any-service'> was not expected

If I remove
xmlns="http://ogsa.globus.org/any-service
from the xml file everything works fine.
What could be the reason? Thanks for any help

Andre
Nov 12 '05 #1
4 16481
Andre,

What does the class that you instantiate the serializer for look like?

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Andre" <an*******@disc ussions.microso ft.com> wrote in message
news:95******** *************** ***********@mic rosoft.com...
Hello,

I want to deserialize the following xml:

<anyRoot xmlns:xsd="http ://www.w3.org/2001/XMLSchema" xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xmlns="http://ogsa.globus.org/any-service"><QName >abc</QName></anyRoot>
using:

TextReader tr = new StreamReader("c :\\test.xml");
anyRootType rootType = (anyRootType)se rializer.Deseri alize(tr);
tr.Close();

but always get the exception:

Message: There is an error in XML document (1, 2).
Message: <anyRoot xmlns='http://ogsa.globus.org/any-service'> was not expected.
If I remove:
xmlns="http://ogsa.globus.org/any-service"
from the xml file everything works fine.
What could be the reason? Thanks for any help.

Andre

Nov 12 '05 #2
Christoph,

Here is the full code listing. As mentioned before everything works fine as long as the xmlns="http://ogsa.globus.org/agreement-factory-service" is NOT included in the xml.
Thanks for any comments.

Andre

test.xml:
<SupportedAgree mentType xmlns:xsd="http ://www.w3.org/2001/XMLSchema" xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance" xmlns="http://ogsa.globus.org/agreement-factory-service"><QName >agreement01</QName><price><Q Name>price_per_ invocation</QName></price><serviceL evel><QName>ser vice_level</QName></serviceLevel><q osLevel><QName> qos_level</QName></qosLevel><minRe quests><QName>m inimal_requests </QName></minRequests></SupportedAgreem entType>

The c# code:

class Class1
{
[STAThread]
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(t ypeof(Supported AgreementType)) ;
TextReader tr = new StreamReader("c :\\test.xml");
SupportedAgreem entType sat = (SupportedAgree mentType)serial izer.Deserializ e(tr);
tr.Close();
}
}

public class SupportedAgreem entType
{
private string _qname;
SupportedTermTy pe _price;
SupportedTermTy pe _serviceLevel;
SupportedTermTy pe _qosLevel;
SupportedTermTy pe _minRequests;

public string QName
{
get{ return _qname; }
set{ _qname = value; }
}

public SupportedTermTy pe price
{
get{ return _price; }
set{ _price = value; }
}

public SupportedTermTy pe serviceLevel
{
get{ return _serviceLevel; }
set{ _serviceLevel = value; }
}

public SupportedTermTy pe qosLevel
{
get{ return _qosLevel; }
set{ _qosLevel = value; }
}

public SupportedTermTy pe minRequests
{
get{ return _minRequests; }
set{ _minRequests = value; }
}
}

public class SupportedTermTy pe
{
private string _qname;
private string _termValue;

public string QName
{
get{ return _qname; }
set{ _qname = value; }
}

public string termValue
{
get{ return _termValue; }
set{ _termValue = value; }
}
}

Nov 12 '05 #3
The serializer doesn't know about the Xml namespace since the .NET Type
doesn't declare it.

You need to add the following attributes to make sure the serializer will
consider the namespace:

[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://ogsa.globus.org
/agreement-factory-service")]
[System.Xml.Seri alization.XmlRo otAttribute("Su pportedAgreemen tType",
Namespace="http ://ogsa.globus.org/agreement-factory-service",
Nullable=false)]

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Andre" <an*******@disc ussions.microso ft.com> wrote in message
news:9B******** *************** ***********@mic rosoft.com...
Christoph,

Here is the full code listing. As mentioned before everything works fine as long as the xmlns="http://ogsa.globus.org/agreement-factory-service" is
NOT included in the xml. Thanks for any comments.

Andre

test.xml:
<SupportedAgree mentType xmlns:xsd="http ://www.w3.org/2001/XMLSchema" xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xmlns="http://ogsa.globus.org/agreement-factory-service"><QName >agreement01<
/QName><price><Q Name>price_per_ invocation</QName></price><serviceL evel><QNam
e>service_level </QName></serviceLevel><q osLevel><QName> qos_level</QName></qo
sLevel><minRequ ests><QName>min imal_requests</QName></minRequests></Supported
AgreementType>
The c# code:

class Class1
{
[STAThread]
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(t ypeof(Supported AgreementType)) ; TextReader tr = new StreamReader("c :\\test.xml");
SupportedAgreem entType sat = (SupportedAgree mentType)serial izer.Deserializ e(tr); tr.Close();
}
}

public class SupportedAgreem entType
{
private string _qname;
SupportedTermTy pe _price;
SupportedTermTy pe _serviceLevel;
SupportedTermTy pe _qosLevel;
SupportedTermTy pe _minRequests;

public string QName
{
get{ return _qname; }
set{ _qname = value; }
}

public SupportedTermTy pe price
{
get{ return _price; }
set{ _price = value; }
}

public SupportedTermTy pe serviceLevel
{
get{ return _serviceLevel; }
set{ _serviceLevel = value; }
}

public SupportedTermTy pe qosLevel
{
get{ return _qosLevel; }
set{ _qosLevel = value; }
}

public SupportedTermTy pe minRequests
{
get{ return _minRequests; }
set{ _minRequests = value; }
}
}

public class SupportedTermTy pe
{
private string _qname;
private string _termValue;

public string QName
{
get{ return _qname; }
set{ _qname = value; }
}

public string termValue
{
get{ return _termValue; }
set{ _termValue = value; }
}
}

Nov 12 '05 #4
Thanks Christoph for the quick help, It works fine now. Best regards, Andre
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8332
by: David B. Bitton | last post by:
I am having a problem deserializing XML when the root node is missing a namespace declaration. My Type has an XmlTypeAttribute with a namespace defined. If I attempt to deserialize the XML, I get the dreaded <elementname xmlns=''> was not expected exception. If I comment out the XmlTypeAttribute, it works just fine. Just so you know,...
4
11790
by: Sebastien Tardif | last post by:
Subject: XmlSerializer.Deserialize complain when root declare the namespace If I do XmlSerializer.Deserialize( myString ) and myString is: String myString = "<?xml version=\"1.0\" encoding=\"utf-16\"?><DocumentResponse ><documentSize xmlns=\"urn:webservices.docharbor.com\">23</documentSize></DocumentResponse>"; It's working
5
10046
by: Alexis | last post by:
Hello, I have a set of classes I created from schema files using the xsd.exe tool. I'm using namespaces in the clases ( I had to because I have some classes with the same name but not the same class ) Here is how a given class wil looks like. <System.Xml.Serialization.XmlRootAttribute(:="mynamespace", IsNullable:=False)> _ Public Class...
4
2407
by: Peter Speybrouck | last post by:
I have a little problem with a webservice. I reproduced the problem in the following simplified example. I just create a new C# ASP.NET webservice and a c# console application. I added a new class test to the namespace of the webservice which I try to access from the console application. //Service1.asmx.cs
1
2264
by: Farouche | last post by:
Hi all I have made two simple methods to serialize/deserialize a simple data structure to my database using SoapFormatting. This actually works just great, when the structures stays the same. BUT if I change the structure, say adds a field, I get an error when trying to deserialize the allready serialized object from the database saying...
4
1842
by: PaulF | last post by:
How do I identify all of the namespace / prefix pairs associated with an XML document I am reading? Thanks for any help. Paul
3
1678
by: Massimo Gentilini | last post by:
I have a problem with namespace: I've a class in a namespace namespace.Foo that's used in a web method when I reference the web method from a web service the namespace changes because of the wsdl compiler, so now my class, in the code using the web
6
3814
by: pamela fluente | last post by:
Hi, please find below a very simple code snippet which is giving me the following error: System.Runtime.Serialization.SerializationException was unhandled Message="The constructor to deserialize an object of type 'WindowsApplication10.Form1+DictionaryExt`2' was not found." Source="mscorlib" You can just past the snippet on any form with...
2
2484
by: =?Utf-8?B?SWFpbg==?= | last post by:
I've created some c# classes from Xsds. The classes have namespace attributes in the XmlRootAttributes. Accordingly, they will NOT deserialize with the XmlSerializer unless the same namespace is defined. I've prepared a temporary fix by loading the xml as an XmlDocument, amending the first element to include the namespace, serialising...
0
7807
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7730
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8058
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8241
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7820
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5301
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1061
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.