473,403 Members | 2,323 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,403 software developers and data experts.

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)serializer.Deserialize(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 16459
Andre,

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

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

"Andre" <an*******@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.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)serializer.Deserialize(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:
<SupportedAgreementType 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><QName>price_per_invocation</QName></price><serviceLevel><QName>service_level</QName></serviceLevel><qosLevel><QName>qos_level</QName></qosLevel><minRequests><QName>minimal_requests</QName></minRequests></SupportedAgreementType>

The c# code:

class Class1
{
[STAThread]
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(SupportedAgreementType));
TextReader tr = new StreamReader("c:\\test.xml");
SupportedAgreementType sat = (SupportedAgreementType)serializer.Deserialize(tr) ;
tr.Close();
}
}

public class SupportedAgreementType
{
private string _qname;
SupportedTermType _price;
SupportedTermType _serviceLevel;
SupportedTermType _qosLevel;
SupportedTermType _minRequests;

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

public SupportedTermType price
{
get{ return _price; }
set{ _price = value; }
}

public SupportedTermType serviceLevel
{
get{ return _serviceLevel; }
set{ _serviceLevel = value; }
}

public SupportedTermType qosLevel
{
get{ return _qosLevel; }
set{ _qosLevel = value; }
}

public SupportedTermType minRequests
{
get{ return _minRequests; }
set{ _minRequests = value; }
}
}

public class SupportedTermType
{
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.Serialization.XmlTypeAttribute(Namespac e="http://ogsa.globus.org
/agreement-factory-service")]
[System.Xml.Serialization.XmlRootAttribute("Support edAgreementType",
Namespace="http://ogsa.globus.org/agreement-factory-service",
Nullable=false)]

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

"Andre" <an*******@discussions.microsoft.com> wrote in message
news:9B**********************************@microsof t.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:
<SupportedAgreementType 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><QName>price_per_invocation</QName></price><serviceLevel><QNam
e>service_level</QName></serviceLevel><qosLevel><QName>qos_level</QName></qo
sLevel><minRequests><QName>minimal_requests</QName></minRequests></Supported
AgreementType>
The c# code:

class Class1
{
[STAThread]
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(SupportedAgreementType)); TextReader tr = new StreamReader("c:\\test.xml");
SupportedAgreementType sat = (SupportedAgreementType)serializer.Deserialize(tr) ; tr.Close();
}
}

public class SupportedAgreementType
{
private string _qname;
SupportedTermType _price;
SupportedTermType _serviceLevel;
SupportedTermType _qosLevel;
SupportedTermType _minRequests;

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

public SupportedTermType price
{
get{ return _price; }
set{ _price = value; }
}

public SupportedTermType serviceLevel
{
get{ return _serviceLevel; }
set{ _serviceLevel = value; }
}

public SupportedTermType qosLevel
{
get{ return _qosLevel; }
set{ _qosLevel = value; }
}

public SupportedTermType minRequests
{
get{ return _minRequests; }
set{ _minRequests = value; }
}
}

public class SupportedTermType
{
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
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...
4
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\"...
5
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...
4
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...
1
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....
4
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
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...
6
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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,...
0
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,...
0
tracyyun
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...
0
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,...
0
isladogs
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.