I have created a WCF web service and generated the xml serialization class with xsd.exe. I have two problems when I request the xsd from the service (http://localhost:59120/Service1.svc?xsd=xsd2).
Firstly attributes are returned as elements and secondly the element names are appended with "Field" does anyone know how to fix either of these?
Attributes are being shown as elements -
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Id {
get {
return this.idField;
}
set {
this.idField = value;
}
}
Snippet from ...service1.svc?xsd=xsd2 showing element names appended with "Field" and "IdField" should be an attribute not an element
I wonder if you use an option "/f[ields]" when generate xsd with xsd.exe so that it generates fields instead of properties. By default, properties are generated.
Sadly my attribute (<xs:element name="id".../>) is still being returned as an element. It's weird that the processor seems to be ignoring the "[System.Xml.Serialization.XmlAttributeAttribute()] "
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id;
I believe that I've fixed this by changing the default serializer. From .Net Framwork 3.0 microsoft changed the default serializer to the DataContract serializer, which is different from the XmlSerializer that ASMX uses.
"Therefore, the XSD generated varies, too - the DataCOntract serializer for example doesn't support XML attributes (for speed reasons)."
I specified the XmlSerializer in the service interface [Contract] last night and it seems to be producing the expected xsd now. I did this late last night so haven't tested it properly yet and I'm off now until the new year.
Here are some links to the thread where I found the info I've been looking for -
RunningBeare
Member
1 Points
3 Posts
VS2010: WCF Service using XML serialization - xsd schema output
Dec 19, 2012 09:20 AM|LINK
Hi,
I have created a WCF web service and generated the xml serialization class with xsd.exe. I have two problems when I request the xsd from the service (http://localhost:59120/Service1.svc?xsd=xsd2). Firstly attributes are returned as elements and secondly the element names are appended with "Field" does anyone know how to fix either of these?
Attributes are being shown as elements -
[System.Xml.Serialization.XmlAttributeAttribute()] public string Id { get { return this.idField; } set { this.idField = value; } }Snippet from ...service1.svc?xsd=xsd2 showing element names appended with "Field" and "IdField" should be an attribute not an element
C# Class -
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.296 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ // // This source code was auto-generated by xsd, Version=4.0.30319.1. // namespace WcfService1 { using System.Xml.Serialization; /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] public partial class Orders { private OrdersCustomer[] itemsField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("Customer", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)] public OrdersCustomer[] Items { get { return this.itemsField; } set { this.itemsField = value; } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class OrdersCustomer { private string nameField; private string addressField; private string emailIdField; private OrdersCustomerItemsItem[][] itemsField; private string idField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)] public string Name { get { return this.nameField; } set { this.nameField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=1)] public string Address { get { return this.addressField; } set { this.addressField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=2)] public string EmailId { get { return this.emailIdField; } set { this.emailIdField = value; } } /// <remarks/> [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=3)] [System.Xml.Serialization.XmlArrayItemAttribute("Item", typeof(OrdersCustomerItemsItem), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] public OrdersCustomerItemsItem[][] Items { get { return this.itemsField; } set { this.itemsField = value; } } /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public string Id { get { return this.idField; } set { this.idField = value; } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class OrdersCustomerItemsItem { private string idField; private string nameField; /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public string Id { get { return this.idField; } set { this.idField = value; } } /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public string Name { get { return this.nameField; } set { this.nameField = value; } } } }Any help will be greatly appreciated.
oak_silver
Member
418 Points
64 Posts
Re: VS2010: WCF Service using XML serialization - xsd schema output
Dec 20, 2012 06:32 AM|LINK
I wonder if you use an option "/f[ields]" when generate xsd with xsd.exe so that it generates fields instead of properties. By default, properties are generated.
http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.100).aspx
RunningBeare
Member
1 Points
3 Posts
Re: VS2010: WCF Service using XML serialization - xsd schema output
Dec 20, 2012 08:53 AM|LINK
Oak_Silver,
Thanks for your reply your suggestion has indeed removed the appended "Field" text, thanks!!
Sadly my attribute (<xs:element name="id".../>) is still being returned as an element. It's weird that the processor seems to be ignoring the "[System.Xml.Serialization.XmlAttributeAttribute()] "
Haixia Xie -...
Contributor
3030 Points
296 Posts
Microsoft
Re: VS2010: WCF Service using XML serialization - xsd schema output
Dec 21, 2012 02:18 AM|LINK
Hi,
Please take a look at responses in this thread.
Best Regards.
Feedback to us
Develop and promote your apps in Windows Store
RunningBeare
Member
1 Points
3 Posts
Re: VS2010: WCF Service using XML serialization - xsd schema output
Dec 21, 2012 10:11 AM|LINK
Hi,
I believe that I've fixed this by changing the default serializer. From .Net Framwork 3.0 microsoft changed the default serializer to the DataContract serializer, which is different from the XmlSerializer that ASMX uses.
"Therefore, the XSD generated varies, too - the DataCOntract serializer for example doesn't support XML attributes (for speed reasons)."
I specified the XmlSerializer in the service interface [Contract] last night and it seems to be producing the expected xsd now. I did this late last night so haven't tested it properly yet and I'm off now until the new year.
Here are some links to the thread where I found the info I've been looking for -
http://stackoverflow.com/questions/2592664/schema-generated-from-wcf-web-service-and-asmx-are-different?rq=1
Blog post comparing the two serializers
Serialization in WCF