serialization issue.
-
quarta-feira, 28 de março de 2012 14:04
Hi,
using xsd, I generated a class that I'm trying to serialize and deserialize.
The problem is that in serialization and deserialization, all fields contain the word "field".
Ex in the xsd generated class:
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}Ex of data serialized:
<nameField><familyNameField>test</familyNameField></nameField>
It should be :
<name><familyName>test</familyName></name>
How can I achieve this?
Thanks.
Cordialement,
Emmanuel Dreux
http://www.bcpsoft.fr
Formation FIM 2010
Todas as Respostas
-
quarta-feira, 28 de março de 2012 14:26Moderador
Where is "familyname" coming from?
Based upon the property that you posted, the node should look like this.
<name>test</name>
I have also found that the xsd.exe tool is not perfect. It works well under simple scenarios, but it only offers a starting point for more complex stuff. You might also be interested in the following link.
http://support.microsoft.com/kb/327071 FIX: Qualified Attributes in XML Schemas Do Not Serialize Correctly
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
-
quarta-feira, 28 de março de 2012 19:50
Yes,
I tried this kb in vain. (And you're right for the typo, I built this example by hands.)
You should read:
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}is serialzed as <nameField>test</nameField>.
I want it to be serialized as <name>test</name>
The problem is that it serializes the field (namefield) instead of the property (name).
Is there a way to change this behavior?
Cordialement,
Emmanuel Dreux
http://www.bcpsoft.fr
Formation FIM 2010 -
quarta-feira, 28 de março de 2012 21:03Moderador
can you post a simplified portion of the xsd that exhibits the problem?
gimme some slamming techno!!!!
-
segunda-feira, 2 de abril de 2012 01:47Moderador
-
domingo, 8 de abril de 2012 21:03
Hi,
I'm back on this thread.
Here is the xsd: http://scim.googlecode.com/svn/trunk/website/specs/schema/scim-core.xsd
I used xsd to generate the class.
It generated this kind of code that serialized as <nameField>test</nameField> instead of <name>test</name>
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}Thanks.
Cordialement,
Emmanuel Dreux
http://www.bcpsoft.fr
Formation FIM 2010 -
segunda-feira, 9 de abril de 2012 02:25Moderador
Hi,
Thanks for following up! I did some test on the xsd file you provided, however, the generated class codes will serialize as the format like <name>test</name> correctly, and I did not repro the problem you mentioned. Could you please provide more sample codes about how you do the XML serialization?
Thanks
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
-
quarta-feira, 11 de abril de 2012 09:59
That's a good news that you serialize it correctly.
Can you post the syntax that you used for generating the cs class using xsd?
Here is the function that serializes:
public HttpContent CreateResourceDataContract(TResource resource, WebContentFormat format = WebContentFormat.Json)
{
switch (format)
{
case WebContentFormat.Xml:
return HttpContentExtensions.CreateDataContract<TResource>(resource);
case WebContentFormat.Json:
return HttpContentExtensions.CreateJsonDataContract<TResource>(resource);
default:
throw new NotImplementedException("Unsupported WebContentFormat");
}
}Object serialized:
var _User = new User(); // User is the class generated from the xsd
_User.name = new name();
_User.name.givenName = firstName;
_User.userName = loginName;
_User.name.familyName = lastName;And the call:
public ResourceResult<TResource> SendRequest(
string method,
Uri requestUri,
WebContentFormat format = WebContentFormat.Json,
TResource resource = default(TResource), <<<----- the user class is passed
Action<HttpRequestMessage> beforeRequest = null,
Action<HttpResponseMessage> afterResponse = null)
{
var result = new ResourceResult<TResource>();
using (HttpClient client = new HttpClient())
{
using (HttpRequestMessage request = new HttpRequestMessage(method, requestUri))
{
request.Headers.Accept.Add(WebContentFormatString(format));
if (resource != null)
request.Content = CreateResourceDataContract(resource, format);Cordialement,
Emmanuel Dreux
http://www.bcpsoft.fr
Formation FIM 2010 -
quinta-feira, 12 de abril de 2012 04:15Moderador
Hi,
I created the .cs class file by the xsd command, http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.100).aspx. I downloaded the xsd file you shared and name it as myXsd.xsd. Then I used this command xsd myXsd.xsd /c /l:CS, and the myXsd.cs file will be generated.
Are you using Json in your codes? I don't know whether there will be any difference, but I just use some regular XmlSerializer class and here are codes:
XmlSerializer x = new XmlSerializer(typeof(User));
User user = new User();
user.name = new name();
user.name.givenName = "Michael";
user.userName = "misun";
user.name.familyName = "Sun";
x.Serialize(Console.Out, user);
Console.WriteLine();Good day!
Thanks
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
-
quinta-feira, 12 de abril de 2012 08:50
We're making progress, thanks.
I serialized the same object using your code and using HttpContentExtensions.CreateDataContract or HttpContentExtensions.CreateJsonDataContract.
Results are different.
Your code (XmlSerializer) generates indeed the correct output:
<?xml version="1.0" encoding="ibm850"?>
<User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://ww
w.w3.org/2001/XMLSchema" xmlns="urn:scim:schemas:core:1.0">
<userName xmlns="">misun</userName>
<name xmlns="">
<familyName>Sun</familyName>
<givenName>Michael</givenName>
</name>
</User>HttpContentExtensions.CreateDataContract<TResource>(resource) generates the following incorrect output:
<User xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/"><idField i:nil="true" /><metaField i:nil="true" /><externalIdField i:nil="true" /><activeField>false</activeField><activeFieldSpecified>false</activeFieldSpecified><addressesField i:nil="true" /><anyField xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Xml" i:nil="true" /><displayNameField i:nil="true" /><emailsField i:nil="true" /><entitlementsField i:nil="true" /><groupsField i:nil="true" /><imsField i:nil="true" /><localeField i:nil="true" /><nameField><familyNameField>Sun</familyNameField><formattedField i:nil="true" /><givenNameField>Michael</givenNameField><honorificPrefixField i:nil="true" /><honorificSuffixField i:nil="true" /><middleNameField i:nil="true" /></nameField><nickNameField i:nil="true" /><passwordField i:nil="true" /><phoneNumbersField i:nil="true" /><photosField i:nil="true" /><preferredLanguageField i:nil="true" /><profileUrlField i:nil="true" /><rolesField i:nil="true" /><timezoneField i:nil="true" /><titleField i:nil="true" /><userNameField>misun</userNameField><userTypeField i:nil="true" /><x509CertificatesField i:nil="true" /></User>
Cordialement,
Emmanuel Dreux
http://www.bcpsoft.fr
Formation FIM 2010 -
sexta-feira, 13 de abril de 2012 02:31Moderador
Hi,
Are you using WCF? I cannot see your detailed code logic, but I think it may be related to two serialization ways WCF can use, http://msdn.microsoft.com/en-us/library/ms733901.aspx.
Or can you show the detailed logic of HttpContentExtensions.CreateDataContract or HttpContentExtensions.CreateJsonDataContract?
Have a nice weekend!
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
-
sábado, 14 de abril de 2012 13:39
It's Microsoft code in Microsoft.http.HttpContentExtensions
Cordialement,
Emmanuel Dreux
http://www.bcpsoft.fr
Formation FIM 2010 -
segunda-feira, 16 de abril de 2012 02:12Moderador
Hi,
Are you using .NET 3.5 and the WCF REST Starter Kit? I don't know why the API you use generates different results, but this library is no longer supported. For building RESTful services on .NET, we use ASP.NET Web API now.
Or if you can use .NET 4.0 WCF, I think DataContractSerializer and XmlSerializer are both good to use, http://msdn.microsoft.com/en-us/library/ms733901.aspx & http://msdn.microsoft.com/en-us/library/ms731073.aspx. Besides, if you have any questions regarding WCF, you can post here, http://social.msdn.microsoft.com/Forums/en-US/wcf/threads.
Good day!
Thanks
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
- Sugerido como Resposta Michael Sun [MSFT]Microsoft Employee, Moderator quarta-feira, 25 de abril de 2012 01:26
- Marcado como Resposta Michael Sun [MSFT]Microsoft Employee, Moderator terça-feira, 1 de maio de 2012 01:19
-
quinta-feira, 19 de abril de 2012 01:34Moderador
Hi,
Any update of this issue? If you have any questions, please feel free to let me know.
Good day!
Thanks
Michael Sun [MSFT]
MSDN Community Support | Feedback to us

