473,480 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

deserialising xml files with no namespace

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.

But surely there is an easier way....

Iain
Jun 27 '08 #1
2 2473
Iain wrote:
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.

But surely there is an easier way....
Well the easiest way is to change the XmlRootAttribute.
If you can't do that then using an XmlParserContext as follows does work
for me:

string xml = "<Foo><Bar>baz</Bar></Foo>";
XmlSerializer ser = new XmlSerializer(typeof(Foo));

NameTable nt = new NameTable();
XmlNamespaceManager mgr = new XmlNamespaceManager(nt);
mgr.AddNamespace("", "http://example.com/2008/ns1");
XmlParserContext ctxt = new XmlParserContext(nt, mgr, "",
XmlSpace.Default);

Foo foo = (Foo)ser.Deserialize(XmlReader.Create(new
StringReader(xml), null, ctxt));
Console.WriteLine("foo.Bar: {0}", foo.Bar);

where the Foo class is defined as follows:

[XmlRoot(Namespace = "http://example.com/2008/ns1")]
public class Foo
{
public string Bar { get; set; }
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
"Iain" <Ia**@discussions.microsoft.comwrote in message
news:D5**********************************@microsof t.com...
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.

But surely there is an easier way....

Iain
As well as Martin's suggestion why does the XML to be deserialised not have
the namespaced elements?

--

Joe Fawcett (MVP - XML)

http://joe.fawcett.name
Jun 27 '08 #3

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

Similar topics

2
3843
by: maddalimurali | last post by:
Hello all, My xml and schema file header are as follows. 1) cisGlobals.xsd. <?xml version="1.0" encoding="UTF-8"?> <xsd:schema targetNamespace="http://www.talgov.com/cisGlobals"...
0
1233
by: Rico Rivera | last post by:
I have two files: FileA.vb FileB.vb FileA.vb contains the following sample code: NameSpace SomeNamespace Public Class SomeClassA Public Shared Function MethodA() As String
6
4297
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
2
1932
by: db | last post by:
Hey group I'm rewriting a small program called lockdown and need a little help structuring the code. In the past I didn't use nested namespaces and I placed all my files in one directory. This...
0
1276
by: ORC | last post by:
Applies to C#, Visual Studio 2003 version 7.1.3088. I have a project where I have several linked files included. One problem is that when I make changes in one of the linked file I will have to...
3
3331
by: Dan | last post by:
Hi, I have a problem using an aspx page with a Control on it. I get the following error message Compiler Error Message: CS1595: 'Test.Class2' is defined in multiple places; using definition...
7
2221
by: Matt Jensen | last post by:
Howdy I want to simulate with .Net what I used to do with classic ASP where you would have a series of include files with utility functions that you would include when you needed them. I read...
0
934
by: msustrick | last post by:
I'm working on a large asp.net maintenance application that has numerous utilities. There can be multiple files associated with a particular utility. The Namespace standard is...
36
3779
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
0
7046
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
6908
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...
1
6741
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...
0
4485
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...
0
2997
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...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
183
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...

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.