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
0
Iain1263 (24)
6/24/2008 9:17:00 AM
dotnet.xml 7266 articles. 0 followers. Follow

2 Replies
704 Views

Similar Articles

[PageSpeed] 53

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/
0
mahotrash (1778)
6/24/2008 3:38:33 PM
"Iain" <Iain@discussions.microsoft.com> wrote in message 
news:D567B0A2-F2C7-47D9-9CAB-E3448349B00A@microsoft.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


0
6/25/2008 7:08:55 AM
Reply:

Similar Artilces:

Import XML into Word Document
Hello, today I read an Article on MSDN on how to create Word templates that use XML schemas. The Articel is called "Creating and Applying an XML Resume Template in Microsoft Office Word 2003" (http://msdn.microsoft.com/office/understanding/xmloffice/codesamples/default.aspx?pull=/library/en-us/odc_wd2003_ta/html/odc_wdxmlresumetemp.asp) The features described are very nice, at least the option to export the data as XML. Now I am searching for a way to import this data into my template. Is there a way to do this? I understand that the saved XML can be used in other applications...

create card file from csv file
how can i create card file from a csv What is a "card" file. CSV is a text file. barb60185 wrote: > how can i create card file from a csv > ...

archived files
How do I access the archived files from Outlook if I want to go back and look at the data that was saved (i.e. old e-mails and/or calendar appointments etc.) In OL File/Open/Personal Folders......browse to archive "Yvonne" <anonymous@discussions.microsoft.com> wrote in message news:1a4bc01c41d81$ebb52340$a101280a@phx.gbl... > How do I access the archived files from Outlook if I want > to go back and look at the data that was saved (i.e. old > e-mails and/or calendar appointments etc.) Thanks DL, it worked and I found what I was looking for. Appreciate the help...

File Reservation in XP
When I attempt to open an Excel spreadsheet by double-clicking the file I get the File Reservation window, saying that the file is bein modified by me. If I choose "Cancel", then d-c the file again, i opens fine. Any idea what is causing this? I'm running Excel 97 SR- under XP Pro SP-1. Thanks -- Testing12 ----------------------------------------------------------------------- Testing123's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=1663 View this thread: http://www.excelforum.com/showthread.php?threadid=31498 Maybe... http://www.contextur...

Importing .CSV files into Outlook.......how.........????
I need to import my AOL address book into Outlook, and was told I needed to save the addresses as .CSV files. Unfortunately, I don't know how to do that. If anyone has any ideas or maybe has done it before, Please Help! Ask AOL. --� Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. Due to the SWEN virus, all mail sent to my personal account will be deleted without reading. After searching google.groups.com and finding no answer, Philip asked: | I need to import my AOL address book into Outlook, and | was told I needed to save the addresses...

SQL to XML to XSL
Hi there, I'm a little stuck and would appreciate ANY help getting over this hurdle. Let's say that I've got a SQL Server stored procedure that includes FOR XML AUTO, ELEMENTS And that within a .Net Web Form, I'd like to call that stored procedure and then apply an .XSLT file to it. What would the code look like to do that? Any help would be GREATLY appreciated. Thanks, _K http://www.wiley.com/extras Look for ADO.NET and XML: ASP.NET on the Edge Download the sample code for the last three chapters. There is an example in there on how to do an XSLT transform directl...

opening files created on previous versions
I am trying to open a file created in pub95 version5.0 with pub98. Response- cannot open files created on previous versions. Is there a way around this? Thanks Hi Lloyd (anonymous@discussions.microsoft.com), in the newsgroups you posted: || I am trying to open a file created in pub95 version5.0 with pub98. || Response- cannot open files created on previous versions. Is there a || way around this? Thanks How do you know what the previous version was? FWIW, Publisher 95 (Actually called Publisher for Windows 95) is version 3.0. Version 5.0 is version 98. -- Brian Kvalheim Microsoft Publis...

Print all open files
I have a macro that opens selected files. I would like to add a macro to print them also, but I need to look at them before printing. Basically I need a way to change to next workbook. Is there a command for that or a loop that prints and closes? ...

XML
In order to customize the CRM, is it possible to make changes in the XML I can see the XML commands under the View tab, but can't modify it. Is there a way to do this? If not, can someone please suggest what would be the least complex way to design a new page in CRM. thanx g Simplest way to start is to use ASP.NET and the SDK. Using these tools you can create an integration into Microsoft CRM. It is also possible to use some of the code behind the CRM web server and create pages which more closely match the CRM pages. However this is more advanced. I would check out the sdk on msdn.mic...

Stationary
I have a client company that wants a very specific background color with their graphic logo as the signature file. How to I import this into the signature picker? Or, is there another way to make this HTML file be a stationary for e-mail messages in Outlook 2003? Thank you Create the htm-file first and then add the signature based on that template. Don't forget to place the picture in the Signature folder. By default this is located in; C:\Documents and Settings\%username%\APPLICATION DATA\Microsoft\Stationery -- Roady [MVP] www.sparnaaij.net Microsoft Office and Microsoft Office...

Errror Help: character ' ', hexadecimal value 0x2 is illegal in xml documents
what does this error mean? how can i fix this? thanks, raj * raj wrote in microsoft.public.dotnet.xml: >what does this error mean? how can i fix this? You have an octet 0x02 or perhaps a character reference &#x02; in the document. This is not allowed in XML 1.0; the latter is allowed in XML 1.1 but your version of the .NET framework probably does not support that, and most likely you have not declared this version either. In other words, your document includes random binary data and is there- fore not an XML document. You have to correct the document before you can successfully...

Cannot open vsd file
I am running office 2003. I installed the Visio viewer and compatibility pack (was not sure if it was 2007 or not). But I still cannot open the image. The weird thing is that after I installed the viewer then the diagram was recognized in Windows XP that it was a visio doc. Correct icon showed up next to it. But when I try to open it, it opens IE, then the warning window asks me to accept loading this, I accept and the screen is blank with an X in upper left hand corner. How can I fix/troublshoot this? Thanks, SJMP Yes, even I am facing the same issue. Any help or tho...

Problems with System.Xml
Hi there, I have this snippet: using System.Xml; .... XmlDocument doc = ...; XmlNode foo = doc.CreateElement("foo"); foo.Attributes.Appen(doc.CreateAttribute("id", "23")); my 'foo' tag looks like this: <foo d2p1:id="" d2p1="23"/> My questions are: 1. Why? ;-) 2. How can I get rid of the d2p stuff and have just <foo id="23/>? Thanks in advance, Giulio -- On Nov 16, 3:19=A0pm, Giulio Petrucci <faken...@fakedomain.com> wrote: > Hi there, > > I have this snippet: > > u...

Flat files to XML
Hello all, How can I convert unstructured flat files into structured XML? Are there any scientific articles and tools that handles this subject? Bye, hakhan Check out http://tinyurl.com/4vgn8 creating custom XmlReaders is a nice way to deal with with CSV or Fixe width files, although don't know exactly what you mean by unstructure files.. - jsm3 ----------------------------------------------------------------------- Posted via http://www.mcse.m ----------------------------------------------------------------------- View this thread: http://www.mcse.ms/message1165932.htm ...

Problems with Excel files
I had a Excel file that was worked on using a Office XP machine, and know it is back on my Mac. But know everytime I save I keep on getting this message using Office 98 for Mac. "This file was created using a later version of Microsoft Excel. If you save this file using Microsoft excel 98, information created with features in the later version may be lost." I have tried the following. 1) Saving as a new file and renaming it 2) Saving as Excel 5.x, and then back to Excel 98 3) Deleting the Cell comments that I created in Excel XP, and redoing them in Excel 98 Thanks, J...

XML deserialisation
Hello, I have an issue where I've replaced a single class field with a number of separate fields that allow more fine control. The problem is that my old files, saved using XmlSerialization, still have this old field name in them, and I need to be able to read it and set the new fields appropriately. E.g. the if the old class class MyClass { public int area; } and the new version is now class MyClass { public int width; public int height; } then when executing: XmlTextReader xmlReader = new XmlTextReader(filename); XmlSerializer xs = new XmlSerializer(typeof(MyClass)); MyClas...

VBS writing to excel file
Im using a vbs script to write data to a excell file but keep getting the fillowing error.Unknown error Code 800A03EC. I know the script will work as it has workeb before when small amounts of data are added to a xls file. Are their any limitations to how much data can be added to a cell? Thanks carmen The error is in this section of code. Do While status = 1 row = row+1 for column = 2 to numcolumns excelcolumn=column - 1 ExcelSheet.Application.Cells(row, excelcolumn).Value = oResultSet.GetColumnValue(column) --This line has the error next status = oResultSet.MoveNext numr...

.bmp file vs .cur file
Hi, friends I need to change cursor when mouse moving from one window to another. I know I need to create .cur files for each cursor. However, can I directly use my .bmp file without creating corresponding .cur file, since I may have hundreds of .bmp files? Any reference paper or sample code Thanks a lot. A cursor contains two bitmaps: one to draw the cursor, and one to mask off the area on which the cursor is drawn. You can find out a lot about these file formats in the MSDN. If you have hundreds of bitmaps, perhaps the correct solution is to make hundreds of cursors and then use only t...

When Exporting Contacts via CSV file: Flag for Follow up is not exported
How to export FLAG FOR FOLLOW UP using OUTLOOK 2003 ...

mdd files
my computer does not read mdd files why Are you using MySQL? --- or are you talking about MacDraft files? Have a feeling you're in the wrong newsgroup. -- ~~~~~~~~~~~~~~~~~~ Rob Giordano Microsoft MVP Expression "ruban" <ruban@discussions.microsoft.com> wrote in message news:59035277-52B9-4BDB-927D-ECDEAF3E3310@microsoft.com... > my computer does not read mdd files why ...

Saving/Loading Xml to/from an Xml file
Hi I have a function called GetXml that returns Xml as expected. I then use: Dim saveFile As New SaveFileDialog and the configure saveFile. Having got the file and the Xml, how can I then write the Xml to the file? Also, how could I load the Xml back in to a string using an OpenFileDialog (openFile) or similar? Regards, Carl Gilbert "Carl Gilbert" <cnl@newsgroup.nospam> wrote in message news:OHk4WC5qEHA.628@TK2MSFTNGP09.phx.gbl... > Having got the file and the Xml, how can I then write the Xml to the file? Dim writer As System.IO.StreamWriter writer = ...

Want to remove/disable/re-map shortcut key to the help files
I want to be able to either uninstall or disable the help from opening in Excel as eveytime I press F1 by miss-take the help files open which makes my computer hang for about 5mins while they load. I have looked under Add/Remove programmes and then clicked on the modity section but there isnt any option for removing them. If this cant be done is it possable to remap the shourtcut key to somthing else so that I dont keep hitting it. Im using Office 2000 btw Thanks in advance, Middle --- Message posted from http://www.ExcelForum.com/ Maybe you could a line or two to your personal.xls (...

xml to word format
Does anyone have working code to convert an xml file (with an xslt file) to word format? I've used the code here: http://support.microsoft.com/kb/311461 But I need it to work on a website. That code is for a windows app. Casper wrote: > Does anyone have working code to convert an xml file (with an xslt file) to > word format? > > I've used the code here: http://support.microsoft.com/kb/311461 > But I need it to work on a website. That code is for a windows app. There is also an example in there that shows how to use XslTransform to transform to Response.O...

how to use xmlresolver for doing xml transforms
I'm trying ti use the example in http://support.microsoft.com/default.aspx?scid=kb;en-us;330587#appliesto but doe s not work. I keep getting the error Public WriteOnly Property XmlResolver() as...is obsolete: Pass XmlResolver as Transfer() method. does anyone know of a good example to do this? Tx! You don't have to use an XmlResolver. Simply pass null (or nothing in vb) and you will no longer get the warning. Is just a temporary fix as I don't any source code to hand. Take care, Peter "rocio" <rocio.katsanis@softwareservices.net> wrote in message news:%239H8...

xml
Hi, How can i read an xml file into a struct`s array ??? I need to fill array of structs (one struct for eace xml block in the xml file) like it can be done in dot net xml reader??? Thanks XML has no good array structure representation. One approach is that you create an array of pointers into the DOM and as you iterate over the DOM, you fill the array in with these pointers. Alteratively, you walk the DOM, and at the point where you are sequencing over the array of elements, you would create an array entry, then walk the DOM and fill the entry in from the DOM. If you are using incrementa...