473,769 Members | 5,742 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 16490
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
8348
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, when I instantiate an instance of an XmlSerializer, I pass a default namespace to the ctor. ...
4
11806
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
10080
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 ClassName
4
2420
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
2272
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 that the number of fields of the structure does not match the number of fields in the one I'm...
4
1858
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
1684
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
3827
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 a button.
2
2488
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 this to a string and deserialising to my classes.
0
9586
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7406
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.