XML Deserialization problem

Z

zemnon

Hello guys,

Here is a part of XML I am getting from a huge XML file:

<Task xmlns="http://al-lighting.com/alcheck/Namespace/AL">
<Name>String</Name>
<Script>
<ScriptFilename>String</ScriptFilename>
<ResultFilename>String</ResultFilename>
<Reportstyles>
<Name>String</Name>
<ReportstyleFilename>String</ReportstyleFilename>
<ReportFilenameExtension>String</ReportFilenameExtension>
<SchemaFilename>String</SchemaFilename>
</Reportstyles>
</Script>
</Task>

here is a class that I was generated with xsd.exe according to my xsd
file:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://mynamespace")]
[System.Xml.Serialization.XmlRoot("Task")]
public partial class ALTask
{

private ALTaskName nameField;

private ALScript scriptField;

private ALTaskComment commentField;

/// <remarks/>
public ALTaskName Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}

/// <remarks/>
public ALScript Script {
get {
return this.scriptField;
}
set {
this.scriptField = value;
}
}

/// <remarks/>
public ALTaskComment Comment {
get {
return this.commentField;
}
set {
this.commentField = value;
}
}
}


and I am trying to deserialize it to the class structure using this
code:
string xml = nodes.Current.OuterXml.ToString();
StringReader reader = new StringReader(xml);
XmlSerializer xs = new XmlSerializer(typeof(ALTask));
xs.Serialize(aa, testTask);
ALTask task = (ALTask) xs.Deserialize(reader);

The error shows that the {"<Task xmlns='http://mynamespace'> was not
expected."}


Does anyone know what the problem is? Because I also tried tp
deserialize the string "<Task><Name>String</Name></Task>" and it
works....

Please help, because I am very confused on this stage.

I will appreciate any help.

Best regards,
Zemnon
 
M

Marcin Hoppe

The error shows that the {"<Task xmlns='http://mynamespace'> was not
expected."}

Please note that namespaces don't match. This one (denoted by an
attribute in the class generated by the xsd.exe tool) is different from
the one in the XML document. I have been experiencing deserialization
problems related to XML namespaces quite often.

Best regards!
Marcin
 
Z

zemnon

Hello Marcin,

Thank you for your quick reply.

I have changed the namespace only in company issues in my post and not
in the code. I confirm that the Namespace is the same for both ->
Deserialization class and string with xml data.

Best regards,
Zemnon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top