469,292 Members | 1,294 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

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 15285
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Alexis | last post: by
1 post views Thread by Farouche | last post: by
3 posts views Thread by Massimo Gentilini | last post: by
2 posts views Thread by =?Utf-8?B?SWFpbg==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.