XML Serialization
-
I'm a new to xml serialization and I haven't found much joy on the web. How do i specify arbthree both nullable and a nonNegativeInteger? Or in otherwords have more then 1 attribute type? i tried using them together like the following but I get a compile error [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")] public string arbtwo; [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] public string arbone; [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")] public string arbtwo; public string arbthree;
-
I'm a new to xml serialization and I haven't found much joy on the web. How do i specify arbthree both nullable and a nonNegativeInteger? Or in otherwords have more then 1 attribute type? i tried using them together like the following but I get a compile error [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")] public string arbtwo; [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] public string arbone; [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")] public string arbtwo; public string arbthree;
In the code you posted above, it is correct to apply multiple attributes above the item such as your first example. If the entire example you have above is all in one class, that could be your problem. You are defining arbtwo twice. I tried this but of code and it compiled with no errors:
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] public string arbone; [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")] public string arbtwo; [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")] public string arbthree;
-
In the code you posted above, it is correct to apply multiple attributes above the item such as your first example. If the entire example you have above is all in one class, that could be your problem. You are defining arbtwo twice. I tried this but of code and it compiled with no errors:
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] public string arbone; [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")] public string arbtwo; [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")] public string arbthree;
Sorry it does compile but I get a runtime error. You need to add XmlChoiceIdentifierAttribute to the 'arbtwo' member. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: You need to add XmlChoiceIdentifierAttribute to the 'arbtwo' member.
-
Sorry it does compile but I get a runtime error. You need to add XmlChoiceIdentifierAttribute to the 'arbtwo' member. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: You need to add XmlChoiceIdentifierAttribute to the 'arbtwo' member.
-
Well the syntax of the attribute tags is correct, so your code is not compiling for another reason. Can you paste some more of the code here so we can figure out why it's not?
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; namespace ITCService { /// /// Summary description for Test1. /// public class Test1 : System.Web.Services.WebService { public Test1() { //CODEGEN: This call is required by the ASP.NET Web Services Designer InitializeComponent(); } #region Component Designer generated code //Required by the Web Services Designer private IContainer components = null; /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if(disposing && components != null) { components.Dispose(); } base.Dispose(disposing); } #endregion // WEB SERVICE EXAMPLE // The HelloWorld() example service returns the string Hello World // To build, uncomment the following lines then save and build the project // To test this web service, press F5 [WebMethod] public SomeDetail HelloWorld(InputDetail inputDetail) { return new SomeDetail(); } } public class SomeDetail { public int SomeValue; public SomeDetail() { } } public class InputDetail { [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")] public int InputValue; public InputDetail() { } } }
-
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; namespace ITCService { /// /// Summary description for Test1. /// public class Test1 : System.Web.Services.WebService { public Test1() { //CODEGEN: This call is required by the ASP.NET Web Services Designer InitializeComponent(); } #region Component Designer generated code //Required by the Web Services Designer private IContainer components = null; /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if(disposing && components != null) { components.Dispose(); } base.Dispose(disposing); } #endregion // WEB SERVICE EXAMPLE // The HelloWorld() example service returns the string Hello World // To build, uncomment the following lines then save and build the project // To test this web service, press F5 [WebMethod] public SomeDetail HelloWorld(InputDetail inputDetail) { return new SomeDetail(); } } public class SomeDetail { public int SomeValue; public SomeDetail() { } } public class InputDetail { [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")] public int InputValue; public InputDetail() { } } }
-
I copied and pasted everything you have here into a new class in my .NET environment and it compiled perfectly :confused:
It compiles fine. Its giving me a runtime error when I goto the webservice. Server Error in '/ITCService' Application. -------------------------------------------------------------------------------- You need to add XmlChoiceIdentifierAttribute to the 'InputValue' member. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: You need to add XmlChoiceIdentifierAttribute to the 'InputValue' member. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [InvalidOperationException: You need to add XmlChoiceIdentifierAttribute to the 'InputValue' member.] System.Xml.Serialization.XmlReflectionImporter.CheckAmbiguousChoice(XmlAttributes a, Type accessorType, String accessorName) System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType) System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a, String ns) System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns)
-
It compiles fine. Its giving me a runtime error when I goto the webservice. Server Error in '/ITCService' Application. -------------------------------------------------------------------------------- You need to add XmlChoiceIdentifierAttribute to the 'InputValue' member. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: You need to add XmlChoiceIdentifierAttribute to the 'InputValue' member. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [InvalidOperationException: You need to add XmlChoiceIdentifierAttribute to the 'InputValue' member.] System.Xml.Serialization.XmlReflectionImporter.CheckAmbiguousChoice(XmlAttributes a, Type accessorType, String accessorName) System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType) System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a, String ns) System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns)
Ahhh that makes sense. Well, just out of curiosity, did you do exactly what the error tells you to do? It's not done in the example you pasted. Add the XmlChoiceIdentifierAttribute to the InputValue member See below:
public class InputDetail { [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")] [System.Xml.Serialization.XmlChoiceIdentifierAttribute] public int InputValue; public InputDetail() { } }
-
Ahhh that makes sense. Well, just out of curiosity, did you do exactly what the error tells you to do? It's not done in the example you pasted. Add the XmlChoiceIdentifierAttribute to the InputValue member See below:
public class InputDetail { [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")] [System.Xml.Serialization.XmlChoiceIdentifierAttribute] public int InputValue; public InputDetail() { } }
I still get a runtime exception.
-
I still get a runtime exception.