Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XML Serialization

XML Serialization

Scheduled Pinned Locked Moved C#
questionxmljsonhelp
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Arshad_Ebrahim
    wrote on last edited by
    #1

    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;

    X 1 Reply Last reply
    0
    • A Arshad_Ebrahim

      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;

      X Offline
      X Offline
      XRaheemX
      wrote on last edited by
      #2

      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;
      
      A 1 Reply Last reply
      0
      • X XRaheemX

        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;
        
        A Offline
        A Offline
        Arshad_Ebrahim
        wrote on last edited by
        #3

        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.

        X 1 Reply Last reply
        0
        • A Arshad_Ebrahim

          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.

          X Offline
          X Offline
          XRaheemX
          wrote on last edited by
          #4

          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?

          A 1 Reply Last reply
          0
          • X XRaheemX

            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?

            A Offline
            A Offline
            Arshad_Ebrahim
            wrote on last edited by
            #5

            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() { } } }

            X 1 Reply Last reply
            0
            • A Arshad_Ebrahim

              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() { } } }

              X Offline
              X Offline
              XRaheemX
              wrote on last edited by
              #6

              I copied and pasted everything you have here into a new class in my .NET environment and it compiled perfectly :confused:

              A 1 Reply Last reply
              0
              • X XRaheemX

                I copied and pasted everything you have here into a new class in my .NET environment and it compiled perfectly :confused:

                A Offline
                A Offline
                Arshad_Ebrahim
                wrote on last edited by
                #7

                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)

                X 1 Reply Last reply
                0
                • A Arshad_Ebrahim

                  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)

                  X Offline
                  X Offline
                  XRaheemX
                  wrote on last edited by
                  #8

                  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()
                  		{
                  		}
                  	}
                  
                  A 1 Reply Last reply
                  0
                  • X XRaheemX

                    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()
                    		{
                    		}
                    	}
                    
                    A Offline
                    A Offline
                    Arshad_Ebrahim
                    wrote on last edited by
                    #9

                    I still get a runtime exception.

                    X 1 Reply Last reply
                    0
                    • A Arshad_Ebrahim

                      I still get a runtime exception.

                      X Offline
                      X Offline
                      XRaheemX
                      wrote on last edited by
                      #10

                      Well silly, paste the new exception. It's probably a different exception than before. We'll work them out one by one.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups