Click here to Skip to main content
15,443,549 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey Guys,

Today, I experienced a very strange problem, and I did not find any solution for it:

Let's say I have a Xml Document which looks like this:

    <Text value="Test" />
    <Text value="Tes2t" />
    <Text value="Test" />
    <Text value="Tes2t" />

And I have an XSD which describes the above xml. After pulling it through xsd.exe, i got the following classes (only pseudocode, don't have the code with me):

[XmlRoot(Namespace = ""]
public class RootNode
  public ParentNode[] ParentNode { get; set; }

[XmlRoot(Namespace = ""]
public class ParentNode
  public Text[] Text { get; set; }

[XmlRoot(Namespace = ""]
public class Text
  public string Value { get; set; }

Now I try to deserialize the above Xml (as string) using the XmlSerializer to get the object structure.

The main Problem is, that in the Xsd, there are namespaces declared, which are not available in the Xml. I do not have access to the xml source to add the xmlns attributes there, nor do I want to change the automagically created classes from the xsd.exe.

If I use the XmlSerializer without any additional settings, I get an error that he have an error at the beginning of the document (he don't expected a <RootNode xmlns="" node). If I add a RootNodeAttribute to the constructor of the XmlSerializer (described here: then the deserialization runs whithout an exception, but all properties have their default-values / null values).

Does anyone have a hint for me, how I could solve this problem? It don't mind if I respect the xmlns or not, I just want to get the xml data into a C# object ;)

Update 1:
Testing source is here:[^]

Update 2:
I ended up in creating a new schema for the XML, because I found out that the given XML does not match the given xsd...
Lots of work waiting for me... ;)

Thanks alot, any help is appreciated!
Updated 9-Sep-13 22:30pm

It looks like your C# code of the data model (I'm not saying original XSD, as I never saw it) and the XML sample don't match, as simple as that. You can easily confirm is using Visual Studio which has an XML validator which you can use through the XML editor.

Share this answer
Christoph Keller 9-Sep-13 14:07pm     CRLF
Hey SA, Thanks for your reply. The xml unfortunately was generated using the schema I used to generate the classes.. They match except for the xmlns on the root node, and that is the source of the problem I think. How can I get the XmlSerializer to deserialize the xml data without lookung at the xmlns, or other way around, how can I inject the xmlns attribute onto the xml string? Thank you very much for your help. Best regards, Chris
Sergey Alexandrovich Kryukov 9-Sep-13 14:19pm     CRLF
I cannot be sure unless I see original XSD... —SA
Christoph Keller 9-Sep-13 15:55pm     CRLF
Hey SA, I created a small test-project with both serializing and deserializing. Here is the code (open for pulling). I know, the serialize part is a bit silly using the XmlAttributeOverrides, but this produces exactly the XML I got. And unfortunately it is not possible to make changes to the XML output nor to the XSD scheme. By creating the test-project, I saw a (very hacky) workaround, by adding such overrides for each class defined in the XSD, but this should be really the last solution. Are there any other possibilities to deserialize the XML where there are no namespaces defined? Thanks for your help. Best regards, Chris
Christoph Keller 10-Sep-13 3:04am     CRLF
Ok, you are right. I now tried to implement the overrides, but now the data inside the xml do not exactly match (attribute definied in xml, but defined as normal nodes in xsd). So this will never work... After digging around in the actual code, I found out that the resulting XML is generated using the original xml-data (where the xsd really matches) which will be pulled through an xslt transformation where no output-scheme exists.. Is there any way to generate a xsd scheme for a given xslt (for the output xml)?
Christoph Keller 10-Sep-13 4:26am     CRLF
Ok, we will generate a new schema according the XML output of the transformation we got. There seems no other way around, I just hope to catch all properties correctly.. :) Thanks for your help!
I think what you are looking for is a way to change namespaces, which can be done with an xsl transformation. Google has oodles of answers for you here[^]...
Share this answer
Christoph Keller 10-Sep-13 4:28am     CRLF
Thanks for your answer, this would be a solution, but is not an option with using an xslt transformation for this (because we're trying to refactor the current xslt processing and replace it by standard .net object structure and controls for displaying). But thanks anyway for your appreciated help! Best regards, Chris

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900