XmlSerializer: deserialize against xsd generated class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?=

    XmlSerializer: deserialize against xsd generated class

    Hi,

    I have a schema that has an optional element, fieldTag4000Fie ld. If the
    element is omitted from the XML request, when it is deserialized, it will be
    null when I check it - which is fine.

    What happens when the element is supplied as <fieldTag4000Fi eld/(empty),
    it does not equate to null. I want to be able handle this at the
    deserialization level rahter than in my edits later.

    Is there a way to alter the behavior so when I deserialize it, it is null?

    I also want to add that I am calling the "CanDeserialize " method of the
    XmlSerializer object, passing in the request as a XmlNodeReader.

    Thanks.

    The following C# class snippet was generated by xsd.exe V2:

    [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd", "2.0.50727. 42")]
    [System.Serializ ableAttribute()]
    [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
    [System.Componen tModel.Designer CategoryAttribu te("code")]
    [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
    Namespace="MyNa mespace")]
    public partial class Request_TypeMes sageType {

    private Request_TypeMes sageTypeFieldTa g4000 fieldTag4000Fie ld;

    public Request_TypeMes sageTypeFieldTa g4000 FieldTag4000 {
    get {
    return this.fieldTag40 00Field;
    }
    set {
    this.fieldTag40 00Field = value;
    }
    }
    }

    [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd", "2.0.50727. 42")]
    [System.Serializ ableAttribute()]
    [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
    [System.Componen tModel.Designer CategoryAttribu te("code")]
    [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
    Namespace="MyNa mespace")]
    public partial class Request_TypeMes sageTypeFieldTa g4000 {

    private IDType_Type intermediaryIDC odeField;

    private bool intermediaryIDC odeFieldSpecifi ed;

    private string intermediaryIde ntifierField;

    public IDType_Type IntermediaryIDC ode {
    get {
    return this.intermedia ryIDCodeField;
    }
    set {
    this.intermedia ryIDCodeField = value;
    }
    }


    [System.Xml.Seri alization.XmlIg noreAttribute()]
    public bool IntermediaryIDC odeSpecified {
    get {
    return this.intermedia ryIDCodeFieldSp ecified;
    }
    set {
    this.intermedia ryIDCodeFieldSp ecified = value;
    }
    }


    public string IntermediaryIde ntifier {
    get {
    return this.intermedia ryIdentifierFie ld;
    }
    set {
    this.intermedia ryIdentifierFie ld = value;
    }
    }
    }

    [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd", "2.0.50727. 42")]
    [System.Serializ ableAttribute()]
    [System.Xml.Seri alization.XmlTy peAttribute(Nam espace="MyNames pace")]
    public enum IDType_Type {

    B,

    C,

    D,

    F,

    U,
    }

  • Joe Fawcett

    #2
    Re: XmlSerializer: deserialize against xsd generated class

    Unless you want to implement IXmlSerializabl e then I don't think you can do
    what you want. You can specify that a null element is shown as:
    <myElement xsi:nil="true"/>
    rather than being omitted entirely by adding the XmlElement(IsNu llable =
    true) attribute to the field/property.

    --

    Joe Fawcett (MVP - XML)



    "j.a. harriman" <jeffrey_no_spa m_alias@nospam. nospamwrote in message
    news:697F7C61-82AC-4387-8F04-57D200C7C703@mi crosoft.com...
    Hi,
    >
    I have a schema that has an optional element, fieldTag4000Fie ld. If the
    element is omitted from the XML request, when it is deserialized, it will
    be
    null when I check it - which is fine.
    >
    What happens when the element is supplied as <fieldTag4000Fi eld/(empty),
    it does not equate to null. I want to be able handle this at the
    deserialization level rahter than in my edits later.
    >
    Is there a way to alter the behavior so when I deserialize it, it is null?
    >
    I also want to add that I am calling the "CanDeserialize " method of the
    XmlSerializer object, passing in the request as a XmlNodeReader.
    >
    Thanks.
    >
    The following C# class snippet was generated by xsd.exe V2:
    >
    [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd", "2.0.50727. 42")]
    [System.Serializ ableAttribute()]
    [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
    [System.Componen tModel.Designer CategoryAttribu te("code")]
    [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
    Namespace="MyNa mespace")]
    public partial class Request_TypeMes sageType {
    >
    private Request_TypeMes sageTypeFieldTa g4000 fieldTag4000Fie ld;
    >
    public Request_TypeMes sageTypeFieldTa g4000 FieldTag4000 {
    get {
    return this.fieldTag40 00Field;
    }
    set {
    this.fieldTag40 00Field = value;
    }
    }
    }
    >
    [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd", "2.0.50727. 42")]
    [System.Serializ ableAttribute()]
    [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
    [System.Componen tModel.Designer CategoryAttribu te("code")]
    [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
    Namespace="MyNa mespace")]
    public partial class Request_TypeMes sageTypeFieldTa g4000 {
    >
    private IDType_Type intermediaryIDC odeField;
    >
    private bool intermediaryIDC odeFieldSpecifi ed;
    >
    private string intermediaryIde ntifierField;
    >
    public IDType_Type IntermediaryIDC ode {
    get {
    return this.intermedia ryIDCodeField;
    }
    set {
    this.intermedia ryIDCodeField = value;
    }
    }
    >
    >
    [System.Xml.Seri alization.XmlIg noreAttribute()]
    public bool IntermediaryIDC odeSpecified {
    get {
    return this.intermedia ryIDCodeFieldSp ecified;
    }
    set {
    this.intermedia ryIDCodeFieldSp ecified = value;
    }
    }
    >
    >
    public string IntermediaryIde ntifier {
    get {
    return this.intermedia ryIdentifierFie ld;
    }
    set {
    this.intermedia ryIdentifierFie ld = value;
    }
    }
    }
    >
    [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd", "2.0.50727. 42")]
    [System.Serializ ableAttribute()]
    [System.Xml.Seri alization.XmlTy peAttribute(Nam espace="MyNames pace")]
    public enum IDType_Type {
    >
    B,
    >
    C,
    >
    D,
    >
    F,
    >
    U,
    }
    >

    Comment

    • =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?=

      #3
      Re: XmlSerializer: deserialize against xsd generated class

      Thanks for the answer Joe.

      I also found some examples of using a style sheet and doing a transformation
      (Using XslCompiledTran sform) of the original XmlDocument. I've run a test
      and it seems to work in that the result is as if the original message hadn't
      had them in there. Are there any reasons why not to implement this?

      Also, I tried to locate good "beginner" examples of using IXmlSerializabl e
      that might be similar to what I need to do, but came up short. I would like
      to to look at this in further detail.

      Do you have any links to examples or are you aware if any of the Microsoft
      example downloads (such as SDK) have any?

      Thanks. Jeff

      "Joe Fawcett" wrote:
      Unless you want to implement IXmlSerializabl e then I don't think you can do
      what you want. You can specify that a null element is shown as:
      <myElement xsi:nil="true"/>
      rather than being omitted entirely by adding the XmlElement(IsNu llable =
      true) attribute to the field/property.
      >
      --
      >
      Joe Fawcett (MVP - XML)
      >

      >
      "j.a. harriman" <jeffrey_no_spa m_alias@nospam. nospamwrote in message
      news:697F7C61-82AC-4387-8F04-57D200C7C703@mi crosoft.com...
      Hi,

      I have a schema that has an optional element, fieldTag4000Fie ld. If the
      element is omitted from the XML request, when it is deserialized, it will
      be
      null when I check it - which is fine.

      What happens when the element is supplied as <fieldTag4000Fi eld/(empty),
      it does not equate to null. I want to be able handle this at the
      deserialization level rahter than in my edits later.

      Is there a way to alter the behavior so when I deserialize it, it is null?

      I also want to add that I am calling the "CanDeserialize " method of the
      XmlSerializer object, passing in the request as a XmlNodeReader.

      Thanks.

      The following C# class snippet was generated by xsd.exe V2:

      [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd", "2.0.50727. 42")]
      [System.Serializ ableAttribute()]
      [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
      [System.Componen tModel.Designer CategoryAttribu te("code")]
      [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
      Namespace="MyNa mespace")]
      public partial class Request_TypeMes sageType {

      private Request_TypeMes sageTypeFieldTa g4000 fieldTag4000Fie ld;

      public Request_TypeMes sageTypeFieldTa g4000 FieldTag4000 {
      get {
      return this.fieldTag40 00Field;
      }
      set {
      this.fieldTag40 00Field = value;
      }
      }
      }

      [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd", "2.0.50727. 42")]
      [System.Serializ ableAttribute()]
      [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
      [System.Componen tModel.Designer CategoryAttribu te("code")]
      [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
      Namespace="MyNa mespace")]
      public partial class Request_TypeMes sageTypeFieldTa g4000 {

      private IDType_Type intermediaryIDC odeField;

      private bool intermediaryIDC odeFieldSpecifi ed;

      private string intermediaryIde ntifierField;

      public IDType_Type IntermediaryIDC ode {
      get {
      return this.intermedia ryIDCodeField;
      }
      set {
      this.intermedia ryIDCodeField = value;
      }
      }


      [System.Xml.Seri alization.XmlIg noreAttribute()]
      public bool IntermediaryIDC odeSpecified {
      get {
      return this.intermedia ryIDCodeFieldSp ecified;
      }
      set {
      this.intermedia ryIDCodeFieldSp ecified = value;
      }
      }


      public string IntermediaryIde ntifier {
      get {
      return this.intermedia ryIdentifierFie ld;
      }
      set {
      this.intermedia ryIdentifierFie ld = value;
      }
      }
      }

      [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd", "2.0.50727. 42")]
      [System.Serializ ableAttribute()]
      [System.Xml.Seri alization.XmlTy peAttribute(Nam espace="MyNames pace")]
      public enum IDType_Type {

      B,

      C,

      D,

      F,

      U,
      }
      >
      >
      >

      Comment

      • Joe Fawcett

        #4
        Re: XmlSerializer: deserialize against xsd generated class

        Jeff

        IXmlSerializabl e is reasonably straightforward .
        The newer version needs three methods, ReadXml, WriteXml and one to find the
        Schema which is pointed to by an attribute, I normally just include the
        schema as an embedded resource.
        In ReadXml you get an XmlReader containing the XML and use it to populate
        the object's fields, either reading it directly or loading it into a
        DomDocumnt/XPathDocument if that's easier. WriteXml takes the fields and
        creates an XML document.
        There is an example here: http://www.devx.com/dotnet/Article/29720

        --

        Joe Fawcett (MVP - XML)


        "j.a. harriman" <jeffrey_no_spa m_alias@nospam. nospamwrote in message
        news:046D4993-D7DE-405A-814C-F5B86AC29005@mi crosoft.com...
        Thanks for the answer Joe.
        >
        I also found some examples of using a style sheet and doing a
        transformation
        (Using XslCompiledTran sform) of the original XmlDocument. I've run a test
        and it seems to work in that the result is as if the original message
        hadn't
        had them in there. Are there any reasons why not to implement this?
        >
        Also, I tried to locate good "beginner" examples of using IXmlSerializabl e
        that might be similar to what I need to do, but came up short. I would
        like
        to to look at this in further detail.
        >
        Do you have any links to examples or are you aware if any of the Microsoft
        example downloads (such as SDK) have any?
        >
        Thanks. Jeff
        >
        "Joe Fawcett" wrote:
        >
        >Unless you want to implement IXmlSerializabl e then I don't think you can
        >do
        >what you want. You can specify that a null element is shown as:
        ><myElement xsi:nil="true"/>
        >rather than being omitted entirely by adding the XmlElement(IsNu llable =
        >true) attribute to the field/property.
        >>
        >--
        >>
        >Joe Fawcett (MVP - XML)
        >>
        >http://joe.fawcett.name
        >>
        >"j.a. harriman" <jeffrey_no_spa m_alias@nospam. nospamwrote in message
        >news:697F7C6 1-82AC-4387-8F04-57D200C7C703@mi crosoft.com...
        Hi,
        >
        I have a schema that has an optional element, fieldTag4000Fie ld. If
        the
        element is omitted from the XML request, when it is deserialized, it
        will
        be
        null when I check it - which is fine.
        >
        What happens when the element is supplied as <fieldTag4000Fi eld/>
        (empty),
        it does not equate to null. I want to be able handle this at the
        deserialization level rahter than in my edits later.
        >
        Is there a way to alter the behavior so when I deserialize it, it is
        null?
        >
        I also want to add that I am calling the "CanDeserialize " method of the
        XmlSerializer object, passing in the request as a XmlNodeReader.
        >
        Thanks.
        >
        The following C# class snippet was generated by xsd.exe V2:
        >
        [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
        "2.0.50727. 42")]
        [System.Serializ ableAttribute()]
        [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
        [System.Componen tModel.Designer CategoryAttribu te("code")]
        [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
        Namespace="MyNa mespace")]
        public partial class Request_TypeMes sageType {
        >
        private Request_TypeMes sageTypeFieldTa g4000 fieldTag4000Fie ld;
        >
        public Request_TypeMes sageTypeFieldTa g4000 FieldTag4000 {
        get {
        return this.fieldTag40 00Field;
        }
        set {
        this.fieldTag40 00Field = value;
        }
        }
        }
        >
        [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
        "2.0.50727. 42")]
        [System.Serializ ableAttribute()]
        [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
        [System.Componen tModel.Designer CategoryAttribu te("code")]
        [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
        Namespace="MyNa mespace")]
        public partial class Request_TypeMes sageTypeFieldTa g4000 {
        >
        private IDType_Type intermediaryIDC odeField;
        >
        private bool intermediaryIDC odeFieldSpecifi ed;
        >
        private string intermediaryIde ntifierField;
        >
        public IDType_Type IntermediaryIDC ode {
        get {
        return this.intermedia ryIDCodeField;
        }
        set {
        this.intermedia ryIDCodeField = value;
        }
        }
        >
        >
        [System.Xml.Seri alization.XmlIg noreAttribute()]
        public bool IntermediaryIDC odeSpecified {
        get {
        return this.intermedia ryIDCodeFieldSp ecified;
        }
        set {
        this.intermedia ryIDCodeFieldSp ecified = value;
        }
        }
        >
        >
        public string IntermediaryIde ntifier {
        get {
        return this.intermedia ryIdentifierFie ld;
        }
        set {
        this.intermedia ryIdentifierFie ld = value;
        }
        }
        }
        >
        [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
        "2.0.50727. 42")]
        [System.Serializ ableAttribute()]
        [System.Xml.Seri alization.XmlTy peAttribute(Nam espace="MyNames pace")]
        public enum IDType_Type {
        >
        B,
        >
        C,
        >
        D,
        >
        F,
        >
        U,
        }
        >
        >>
        >>
        >>

        Comment

        • Sprotty

          #5
          Re: XmlSerializer: deserialize against xsd generated class

          Hi Jeff

          The code produced by xsd.exe, copes well with simple schema's, but if
          your working against anything a bit complicated then it can cause
          issues.

          If you continue to have problems then I suggest you take a look at
          Liquid XML Data Binder

          Liquid XML Data Binder is an advanced code generator allowing you to serialize, validate and edit XML documents via a strongly typed API in C#, C++, Java, VB .Net and Visual Basic 6 (COM). Features include validation against the W3C XSD standard, Sample Application generation, and HTML Documentation generation. Supports industry standard XSD schema such as DITA, DocBook, FpML, HL7, OAGIS, OASIS, OpenGIS, XBRL


          This supports much more of the xsd standard, and the objects are more
          strongly typed (it also comes with a free XSD editor).

          Hope this helps

          Regards Simon

          On 23 Jul, 08:00, "Joe Fawcett" <joefawc...@new sgroup.nospamwr ote:
          Jeff
          >
          IXmlSerializabl e is reasonably straightforward .
          The newer version needs three methods, ReadXml, WriteXml and one to find the
          Schema which is pointed to by an attribute, I normally just include the
          schema as an embedded resource.
          In ReadXml you get an XmlReader containing the XML and use it to populate
          the object's fields, either reading it directly or loading it into a
          DomDocumnt/XPathDocument if that's easier. WriteXml takes the fields and
          creates an XML document.
          There is an example here:http://www.devx.com/dotnet/Article/29720
          >
          --
          >
          Joe Fawcett (MVP - XML)http://joe.fawcett.name
          >
          "j.a. harriman" <jeffrey_no_spa m_al...@nospam. nospamwrote in message
          >
          news:046D4993-D7DE-405A-814C-F5B86AC29005@mi crosoft.com...
          >
          >
          >
          Thanks for the answer Joe.
          >
          I also found some examples of using a style sheet and doing a
          transformation
          (Using XslCompiledTran sform) of the original XmlDocument.  I've run atest
          and it seems to work in that the result is as if the original message
          hadn't
          had them in there.  Are there any reasons why not to implement this?
          >
          Also, I tried to locate good "beginner" examples of using IXmlSerializabl e
          that might be similar to what I need to do, but came up short.  I would
          like
          to to look at this in further detail.
          >
          Do you have any links to examples or are you aware if any of the Microsoft
          example downloads (such as SDK) have any?
          >
          Thanks. Jeff
          >
          "Joe Fawcett" wrote:
          >
          Unless you want to implement IXmlSerializabl e then I don't think you can
          do
          what you want. You can specify that a null element is shown as:
          <myElement xsi:nil="true"/>
          rather than being omitted entirely by adding the XmlElement(IsNu llable=
          true) attribute to the field/property.
          >
          --
          >
          Joe Fawcett (MVP - XML)
          >>
          "j.a. harriman" <jeffrey_no_spa m_al...@nospam. nospamwrote in message
          >news:697F7C6 1-82AC-4387-8F04-57D200C7C703@mi crosoft.com...
          Hi,
          >
          I have a schema that has an optional element, fieldTag4000Fie ld.  If
          the
          element is omitted from the XML request, when it is deserialized, it
          will
          be
          null when I check it - which is fine.
          >
          What happens when the element is supplied as <fieldTag4000Fi eld/>
          (empty),
          it does not equate to null.  I want to be able handle this at the
          deserialization level rahter than in my edits later.
          >
          Is there a way to alter the behavior so when I deserialize it, it is
          null?
          >
          I also want to add that I am calling the "CanDeserialize " method of the
          XmlSerializer object, passing in the request as a XmlNodeReader.
          >
          Thanks.
          >
          The following C# class snippet was generated by xsd.exe V2:
          >
             [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
          "2.0.50727. 42")]
             [System.Serializ ableAttribute()]
             [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
             [System.Componen tModel.Designer CategoryAttribu te("code")]
             [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
          Namespace="MyNa mespace")]
             public partial class Request_TypeMes sageType {
          >
                 private Request_TypeMes sageTypeFieldTa g4000 fieldTag4000Fie ld;
          >
                 public Request_TypeMes sageTypeFieldTa g4000 FieldTag4000 {
                     get {
                         return this.fieldTag40 00Field;
                     }
                     set {
                         this.fieldTag40 00Field = value;
                     }
                 }
             }
          >
             [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
          "2.0.50727. 42")]
             [System.Serializ ableAttribute()]
             [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
             [System.Componen tModel.Designer CategoryAttribu te("code")]
             [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
          Namespace="MyNa mespace")]
             public partial class Request_TypeMes sageTypeFieldTa g4000 {
          >
                 private IDType_Type intermediaryIDC odeField;
          >
                 private bool intermediaryIDC odeFieldSpecifi ed;
          >
                 private string intermediaryIde ntifierField;
          >
                 public IDType_Type IntermediaryIDC ode {
                     get {
                         return this.intermedia ryIDCodeField;
                     }
                     set {
                         this.intermedia ryIDCodeField = value;
                     }
                 }
          >
                 [System.Xml.Seri alization.XmlIg noreAttribute()]
                 public bool IntermediaryIDC odeSpecified {
                     get {
                         return this.intermedia ryIDCodeFieldSp ecified;
                     }
                     set {
                         this.intermedia ryIDCodeFieldSp ecified= value;
                     }
                 }
          >
                 public string IntermediaryIde ntifier {
                     get {
                         return this.intermedia ryIdentifierFie ld;
                     }
                     set {
                         this.intermedia ryIdentifierFie ld = value;
                     }
                 }
             }
          >
             [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
          "2.0.50727. 42")]
             [System.Serializ ableAttribute()]
             [System.Xml.Seri alization.XmlTy peAttribute(Nam espace="MyNames pace")]
             public enum IDType_Type {
          >
                 B,
          >
                 C,
          >
                 D,
          >
                 F,
          >
                 U,
             }- Hide quoted text -
          >
          - Show quoted text -

          Comment

          • =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?=

            #6
            Re: XmlSerializer: deserialize against xsd generated class

            Simon,

            Thanks for the link, I will take a look at it as it may be something we
            could use.

            In the meantime, I did find a code snippet that removes the "empty" nodes
            but it removes ALL the empty nodes in the XML and I didn't state (earlier)
            that I want to do this beginning with a particular child node only.

            When I use the following XPath statement in XMLSpy's XPath Evaluator, it
            locates the "empty" node mentioned in the previous posts:
            /parentnode/secondchildnode//*[not(node())]

            When I use the same statement in my C# code, it will not locate any empty
            nodes "under" the "secondchildnod e" (count = 0):
            XmlNodeList element =
            doc.SelectNodes ("/parentnode/secondchildnode//*[not(node())]");

            The plan is to loop through "element" and remove the nodes using the
            "ParentNode.Rem oveChild" of an XmlNode - setting it to each array element,
            etc.

            Any ideas as to why this is not working in .NET VS2005 2.0 framework?

            Thanks.

            Jeff

            "Sprotty" wrote:
            Hi Jeff
            >
            The code produced by xsd.exe, copes well with simple schema's, but if
            your working against anything a bit complicated then it can cause
            issues.
            >
            If you continue to have problems then I suggest you take a look at
            Liquid XML Data Binder
            >
            Liquid XML Data Binder is an advanced code generator allowing you to serialize, validate and edit XML documents via a strongly typed API in C#, C++, Java, VB .Net and Visual Basic 6 (COM). Features include validation against the W3C XSD standard, Sample Application generation, and HTML Documentation generation. Supports industry standard XSD schema such as DITA, DocBook, FpML, HL7, OAGIS, OASIS, OpenGIS, XBRL

            >
            This supports much more of the xsd standard, and the objects are more
            strongly typed (it also comes with a free XSD editor).
            >
            Hope this helps
            >
            Regards Simon
            >
            On 23 Jul, 08:00, "Joe Fawcett" <joefawc...@new sgroup.nospamwr ote:
            Jeff

            IXmlSerializabl e is reasonably straightforward .
            The newer version needs three methods, ReadXml, WriteXml and one to find the
            Schema which is pointed to by an attribute, I normally just include the
            schema as an embedded resource.
            In ReadXml you get an XmlReader containing the XML and use it to populate
            the object's fields, either reading it directly or loading it into a
            DomDocumnt/XPathDocument if that's easier. WriteXml takes the fields and
            creates an XML document.
            There is an example here:http://www.devx.com/dotnet/Article/29720

            --

            Joe Fawcett (MVP - XML)http://joe.fawcett.name

            "j.a. harriman" <jeffrey_no_spa m_al...@nospam. nospamwrote in message

            news:046D4993-D7DE-405A-814C-F5B86AC29005@mi crosoft.com...


            Thanks for the answer Joe.
            I also found some examples of using a style sheet and doing a
            transformation
            (Using XslCompiledTran sform) of the original XmlDocument. I've run a test
            and it seems to work in that the result is as if the original message
            hadn't
            had them in there. Are there any reasons why not to implement this?
            Also, I tried to locate good "beginner" examples of using IXmlSerializabl e
            that might be similar to what I need to do, but came up short. I would
            like
            to to look at this in further detail.
            Do you have any links to examples or are you aware if any of the Microsoft
            example downloads (such as SDK) have any?
            Thanks. Jeff
            "Joe Fawcett" wrote:
            >Unless you want to implement IXmlSerializabl e then I don't think you can
            >do
            >what you want. You can specify that a null element is shown as:
            ><myElement xsi:nil="true"/>
            >rather than being omitted entirely by adding the XmlElement(IsNu llable =
            >true) attribute to the field/property.
            >--
            >Joe Fawcett (MVP - XML)
            >"j.a. harriman" <jeffrey_no_spa m_al...@nospam. nospamwrote in message
            >>news:697F7C 61-82AC-4387-8F04-57D200C7C703@mi crosoft.com...
            Hi,
            I have a schema that has an optional element, fieldTag4000Fie ld. If
            the
            element is omitted from the XML request, when it is deserialized, it
            will
            be
            null when I check it - which is fine.
            What happens when the element is supplied as <fieldTag4000Fi eld/>
            (empty),
            it does not equate to null. I want to be able handle this at the
            deserialization level rahter than in my edits later.
            Is there a way to alter the behavior so when I deserialize it, it is
            null?
            I also want to add that I am calling the "CanDeserialize " method of the
            XmlSerializer object, passing in the request as a XmlNodeReader.
            Thanks.
            The following C# class snippet was generated by xsd.exe V2:
            [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
            "2.0.50727. 42")]
            [System.Serializ ableAttribute()]
            [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
            [System.Componen tModel.Designer CategoryAttribu te("code")]
            [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
            Namespace="MyNa mespace")]
            public partial class Request_TypeMes sageType {
            private Request_TypeMes sageTypeFieldTa g4000 fieldTag4000Fie ld;
            public Request_TypeMes sageTypeFieldTa g4000 FieldTag4000 {
            get {
            return this.fieldTag40 00Field;
            }
            set {
            this.fieldTag40 00Field = value;
            }
            }
            }
            [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
            "2.0.50727. 42")]
            [System.Serializ ableAttribute()]
            [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
            [System.Componen tModel.Designer CategoryAttribu te("code")]
            [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
            Namespace="MyNa mespace")]
            public partial class Request_TypeMes sageTypeFieldTa g4000 {
            private IDType_Type intermediaryIDC odeField;
            private bool intermediaryIDC odeFieldSpecifi ed;
            private string intermediaryIde ntifierField;
            public IDType_Type IntermediaryIDC ode {
            get {
            return this.intermedia ryIDCodeField;
            }
            set {
            this.intermedia ryIDCodeField = value;
            }
            }
            [System.Xml.Seri alization.XmlIg noreAttribute()]
            public bool IntermediaryIDC odeSpecified {
            get {
            return this.intermedia ryIDCodeFieldSp ecified;
            }
            set {
            this.intermedia ryIDCodeFieldSp ecified = value;
            }
            }
            public string IntermediaryIde ntifier {
            get {
            return this.intermedia ryIdentifierFie ld;
            }
            set {
            this.intermedia ryIdentifierFie ld = value;
            }
            }
            }
            [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
            "2.0.50727. 42")]
            [System.Serializ ableAttribute()]
            [System.Xml.Seri alization.XmlTy peAttribute(Nam espace="MyNames pace")]
            public enum IDType_Type {
            B,
            C,
            D,
            F,
            U,
            }- Hide quoted text -
            - Show quoted text -
            >
            >

            Comment

            • Sprotty

              #7
              Re: XmlSerializer: deserialize against xsd generated class


              You could remove the empy elements in the DOM before loading it as you
              suggest.
              Im not seeing a problem with that

              XmlDocument xmlDoc = new XmlDocument();
              xmlDoc.LoadXml( "<parentnode><s econdchildnode> <b/></
              secondchildnode ></parentnode>");
              XmlNodeList xnl = xmlDoc.SelectNo des("//*[not(node())]");
              Debug.Assert(xn l.Count == 1);

              or you could remove it from the object model after its been loaded
              (deserialized).

              If you were using Liquid XML then you may be able to add code in that
              would remove the empty elements as part of the de-serialization.

              Cheers Simon


              On 31 Jul, 21:22, j.a. harriman <jeffrey_no_spa m_al...@nospam. nospam>
              wrote:
              Simon,
              >
              Thanks for the link, I will take a look at it as it may be something we
              could use.
              >
              In the meantime, I did find a code snippet that removes the "empty" nodes
              but it removes ALL the empty nodes in the XML and I didn't state (earlier)
              that I want to do this beginning with a particular child node only.
              >
              When I use the following XPath statement in XMLSpy's XPath Evaluator, it
              locates the "empty" node mentioned in the previous posts:
              /parentnode/secondchildnode//*[not(node())]
              >
              When I use the same statement in my C# code, it will not locate any empty
              nodes "under" the "secondchildnod e" (count = 0):
              XmlNodeList element =
              doc.SelectNodes ("/parentnode/secondchildnode//*[not(node())]");
              >
              The plan is to loop through "element" and remove the nodes using the
              "ParentNode.Rem oveChild" of an XmlNode - setting it to each array element,
              etc.
              >
              Any ideas as to why this is not working in .NET VS2005 2.0 framework?
              >
              Thanks.
              >
              Jeff
              >
              "Sprotty" wrote:
              Hi Jeff
              >
              The code produced by xsd.exe, copes well with simple schema's, but if
              your working against anything a bit complicated then it can cause
              issues.
              >
              If you continue to have problems then I suggest you take a look at
              Liquid XML Data Binder
              >>
              This supports much more of the xsd standard, and the objects are more
              strongly typed (it also comes with a free XSD editor).
              >
              Hope this helps
              >
              Regards Simon
              >
              On 23 Jul, 08:00, "Joe Fawcett" <joefawc...@new sgroup.nospamwr ote:
              Jeff
              >
              IXmlSerializabl e is reasonably straightforward .
              The newer version needs three methods, ReadXml, WriteXml and one to find the
              Schema which is pointed to by an attribute, I normally just include the
              schema as an embedded resource.
              In ReadXml you get an XmlReader containing the XML and use it to populate
              the object's fields, either reading it directly or loading it into a
              DomDocumnt/XPathDocument if that's easier. WriteXml takes the fields and
              creates an XML document.
              There is an example here:http://www.devx.com/dotnet/Article/29720
              >
              --
              >
              Joe Fawcett (MVP - XML)http://joe.fawcett.name
              >
              "j.a. harriman" <jeffrey_no_spa m_al...@nospam. nospamwrote in message
              >
              >news:046D499 3-D7DE-405A-814C-F5B86AC29005@mi crosoft.com...
              >
              Thanks for the answer Joe.
              >
              I also found some examples of using a style sheet and doing a
              transformation
              (Using XslCompiledTran sform) of the original XmlDocument.  I've run a test
              and it seems to work in that the result is as if the original message
              hadn't
              had them in there.  Are there any reasons why not to implement this?
              >
              Also, I tried to locate good "beginner" examples of using IXmlSerializabl e
              that might be similar to what I need to do, but came up short.  Iwould
              like
              to to look at this in further detail.
              >
              Do you have any links to examples or are you aware if any of the Microsoft
              example downloads (such as SDK) have any?
              >
              Thanks. Jeff
              >
              "Joe Fawcett" wrote:
              >
              Unless you want to implement IXmlSerializabl e then I don't think you can
              do
              what you want. You can specify that a null element is shown as:
              <myElement xsi:nil="true"/>
              rather than being omitted entirely by adding the XmlElement(IsNu llable =
              true) attribute to the field/property.
              >
              --
              >
              Joe Fawcett (MVP - XML)
              >>
              "j.a. harriman" <jeffrey_no_spa m_al...@nospam. nospamwrote in message
              >news:697F7C6 1-82AC-4387-8F04-57D200C7C703@mi crosoft.com...
              Hi,
              >
              I have a schema that has an optional element, fieldTag4000Fie ld. If
              the
              element is omitted from the XML request, when it is deserialized, it
              will
              be
              null when I check it - which is fine.
              >
              What happens when the element is supplied as <fieldTag4000Fi eld/>
              (empty),
              it does not equate to null.  I want to be able handle this at the
              deserialization level rahter than in my edits later.
              >
              Is there a way to alter the behavior so when I deserialize it, it is
              null?
              >
              I also want to add that I am calling the "CanDeserialize " methodof the
              XmlSerializer object, passing in the request as a XmlNodeReader.
              >
              Thanks.
              >
              The following C# class snippet was generated by xsd.exe V2:
              >
                 [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
              "2.0.50727. 42")]
                 [System.Serializ ableAttribute()]
                 [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
                 [System.Componen tModel.Designer CategoryAttribu te("code")]
                 [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
              Namespace="MyNa mespace")]
                 public partial class Request_TypeMes sageType {
              >
                     private Request_TypeMes sageTypeFieldTa g4000 fieldTag4000Fie ld;
              >
                     public Request_TypeMes sageTypeFieldTa g4000 FieldTag4000 {
                         get {
                             return this.fieldTag40 00Field;
                         }
                         set {
                             this.fieldTag40 00Field = value;
                         }
                     }
                 }
              >
                 [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
              "2.0.50727. 42")]
                 [System.Serializ ableAttribute()]
                 [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
                 [System.Componen tModel.Designer CategoryAttribu te("code")]
                 [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
              Namespace="MyNa mespace")]
                 public partial class Request_TypeMes sageTypeFieldTa g4000 {
              >
                     private IDType_Type intermediaryIDC odeField;
              >
                     private bool intermediaryIDC odeFieldSpecifi ed;
              >
                     private string intermediaryIde ntifierField;
              >
                     public IDType_Type IntermediaryIDC ode {
                         get {
                             return this.intermedia ryIDCodeField;
                         }
                         set {
                             this.intermedia ryIDCodeField = value;
                         }
                     }
              >
                     [System.Xml.Seri alization.XmlIg noreAttribute()]
                     public bool IntermediaryIDC odeSpecified {
                         get {
                             return this.intermedia ryIDCodeFieldSp ecified;
                         }
                         set {
                             this.intermedia ryIDCodeFieldSp ecified = value;
                         }
                     }
              >
                     public string IntermediaryIde ntifier {
                         get {
                             return this.intermedia ryIdentifierFie ld;
                         }
                         set {
                             this.intermedia ryIdentifierFie ld = value;
                         }
                     }
                 }
              >
                 [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
              "2.0.50727. 42")]
                 [System.Serializ ableAttribute()]
                 [System.Xml.Seri alization.XmlTy peAttribute(Nam espace="MyNames pace")]
                 public enum IDType_Type {
              >
                     B,
              >
                     C,
              >
                     D,
              >
                     F,
              >
                     U,
                 }- Hide quoted text -
              >
              - Show quoted text -

              Comment

              • =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?=

                #8
                Re: XmlSerializer: deserialize against xsd generated class

                Building off your example, here's a respresentation of my actaul XML:
                <parentnode>
                <firstchildnode >
                <a/>
                </firstchildnode>
                <secondchildnod e>
                <b/>
                <c>
                <d/>
                <e>12345</e>
                </c>
                </secondchildnode >
                </parentnode>

                The XPath expression, "//*[not(node())]", removes all "empty" elements. I
                need it to *only* remove "b" & "d" above. "a" would be left alone.

                Thanks.

                Jeff

                "Sprotty" wrote:
                >
                You could remove the empy elements in the DOM before loading it as you
                suggest.
                Im not seeing a problem with that
                >
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml( "<parentnode><s econdchildnode> <b/></
                secondchildnode ></parentnode>");
                XmlNodeList xnl = xmlDoc.SelectNo des("//*[not(node())]");
                Debug.Assert(xn l.Count == 1);
                >
                or you could remove it from the object model after its been loaded
                (deserialized).
                >
                If you were using Liquid XML then you may be able to add code in that
                would remove the empty elements as part of the de-serialization.
                >
                Cheers Simon
                >
                >
                On 31 Jul, 21:22, j.a. harriman <jeffrey_no_spa m_al...@nospam. nospam>
                wrote:
                Simon,

                Thanks for the link, I will take a look at it as it may be something we
                could use.

                In the meantime, I did find a code snippet that removes the "empty" nodes
                but it removes ALL the empty nodes in the XML and I didn't state (earlier)
                that I want to do this beginning with a particular child node only.

                When I use the following XPath statement in XMLSpy's XPath Evaluator, it
                locates the "empty" node mentioned in the previous posts:
                /parentnode/secondchildnode//*[not(node())]

                When I use the same statement in my C# code, it will not locate any empty
                nodes "under" the "secondchildnod e" (count = 0):
                XmlNodeList element =
                doc.SelectNodes ("/parentnode/secondchildnode//*[not(node())]");

                The plan is to loop through "element" and remove the nodes using the
                "ParentNode.Rem oveChild" of an XmlNode - setting it to each array element,
                etc.

                Any ideas as to why this is not working in .NET VS2005 2.0 framework?

                Thanks.

                Jeff

                "Sprotty" wrote:
                Hi Jeff
                The code produced by xsd.exe, copes well with simple schema's, but if
                your working against anything a bit complicated then it can cause
                issues.
                If you continue to have problems then I suggest you take a look at
                Liquid XML Data Binder
                This supports much more of the xsd standard, and the objects are more
                strongly typed (it also comes with a free XSD editor).
                Hope this helps
                Regards Simon
                On 23 Jul, 08:00, "Joe Fawcett" <joefawc...@new sgroup.nospamwr ote:
                Jeff
                IXmlSerializabl e is reasonably straightforward .
                The newer version needs three methods, ReadXml, WriteXml and one to find the
                Schema which is pointed to by an attribute, I normally just include the
                schema as an embedded resource.
                In ReadXml you get an XmlReader containing the XML and use it to populate
                the object's fields, either reading it directly or loading it into a
                DomDocumnt/XPathDocument if that's easier. WriteXml takes the fields and
                creates an XML document.
                There is an example here:http://www.devx.com/dotnet/Article/29720
                --
                Joe Fawcett (MVP - XML)http://joe.fawcett.name
                "j.a. harriman" <jeffrey_no_spa m_al...@nospam. nospamwrote in message
                news:046D4993-D7DE-405A-814C-F5B86AC29005@mi crosoft.com...
                Thanks for the answer Joe.
                I also found some examples of using a style sheet and doing a
                transformation
                (Using XslCompiledTran sform) of the original XmlDocument. I've run a test
                and it seems to work in that the result is as if the original message
                hadn't
                had them in there. Are there any reasons why not to implement this?
                Also, I tried to locate good "beginner" examples of using IXmlSerializabl e
                that might be similar to what I need to do, but came up short. I would
                like
                to to look at this in further detail.
                Do you have any links to examples or are you aware if any of the Microsoft
                example downloads (such as SDK) have any?
                Thanks. Jeff
                "Joe Fawcett" wrote:
                >Unless you want to implement IXmlSerializabl e then I don't think you can
                >do
                >what you want. You can specify that a null element is shown as:
                ><myElement xsi:nil="true"/>
                >rather than being omitted entirely by adding the XmlElement(IsNu llable =
                >true) attribute to the field/property.
                >--
                >Joe Fawcett (MVP - XML)
                >"j.a. harriman" <jeffrey_no_spa m_al...@nospam. nospamwrote in message
                >>news:697F7C 61-82AC-4387-8F04-57D200C7C703@mi crosoft.com...
                Hi,
                I have a schema that has an optional element, fieldTag4000Fie ld. If
                the
                element is omitted from the XML request, when it is deserialized, it
                will
                be
                null when I check it - which is fine.
                What happens when the element is supplied as <fieldTag4000Fi eld/>
                (empty),
                it does not equate to null. I want to be able handle this at the
                deserialization level rahter than in my edits later.
                Is there a way to alter the behavior so when I deserialize it, it is
                null?
                I also want to add that I am calling the "CanDeserialize " method of the
                XmlSerializer object, passing in the request as a XmlNodeReader.
                Thanks.
                The following C# class snippet was generated by xsd.exe V2:
                [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
                "2.0.50727. 42")]
                [System.Serializ ableAttribute()]
                [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
                [System.Componen tModel.Designer CategoryAttribu te("code")]
                [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
                Namespace="MyNa mespace")]
                public partial class Request_TypeMes sageType {
                private Request_TypeMes sageTypeFieldTa g4000 fieldTag4000Fie ld;
                public Request_TypeMes sageTypeFieldTa g4000 FieldTag4000 {
                get {
                return this.fieldTag40 00Field;
                }
                set {
                this.fieldTag40 00Field = value;
                }
                }
                }
                [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
                "2.0.50727. 42")]
                [System.Serializ ableAttribute()]
                [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
                [System.Componen tModel.Designer CategoryAttribu te("code")]
                [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
                Namespace="MyNa mespace")]
                public partial class Request_TypeMes sageTypeFieldTa g4000 {
                private IDType_Type intermediaryIDC odeField;
                private bool intermediaryIDC odeFieldSpecifi ed;
                private string intermediaryIde ntifierField;
                public IDType_Type IntermediaryIDC ode {
                get {
                return this.intermedia ryIDCodeField;
                }
                set {
                this.intermedia ryIDCodeField = value;
                }
                }
                [System.Xml.Seri alization.XmlIg noreAttribute()]
                public bool IntermediaryIDC odeSpecified {
                get {
                return this.intermedia ryIDCodeFieldSp ecified;
                }
                set {
                this.intermedia ryIDCodeFieldSp ecified = value;
                }
                }
                public string IntermediaryIde ntifier {
                get {
                return this.intermedia ryIdentifierFie ld;
                }
                set {
                this.intermedia ryIdentifierFie ld = value;
                }
                }
                }
                [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
                "2.0.50727. 42")]
                [System.Serializ ableAttribute()]
                [System.Xml.Seri alization.XmlTy peAttribute(Nam espace="MyNames pace")]
                public enum IDType_Type {
                B,
                C,
                D,
                F,
                U,
                }- Hide quoted text -
                - Show quoted text -
                >
                >

                Comment

                • =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?=

                  #9
                  Re: XmlSerializer: deserialize against xsd generated class

                  I may have tried to oversimplify my example.

                  The "real" XML has a number of "xmlns:" entries following the "parentnode ".
                  When I remove them & test, this statement,
                  "/parentnode/secondchildnode//*[not(node())]", works as I was expecting.

                  Is there something I need to do with adding namespaces (e.g
                  XmlNamespaceMan ager) or something else that I am unaware of?

                  If there are 5 namespaces in the XML do I need to add a namespace for each
                  one?

                  How are the namespaces used in the XPath statement above?

                  Thanks.

                  Jeff

                  "j.a. harriman" wrote:
                  Building off your example, here's a respresentation of my actaul XML:
                  <parentnode>
                  <firstchildnode >
                  <a/>
                  </firstchildnode>
                  <secondchildnod e>
                  <b/>
                  <c>
                  <d/>
                  <e>12345</e>
                  </c>
                  </secondchildnode >
                  </parentnode>
                  >
                  The XPath expression, "//*[not(node())]", removes all "empty" elements. I
                  need it to *only* remove "b" & "d" above. "a" would be left alone.
                  >
                  Thanks.
                  >
                  Jeff
                  >
                  "Sprotty" wrote:
                  >

                  You could remove the empy elements in the DOM before loading it as you
                  suggest.
                  Im not seeing a problem with that

                  XmlDocument xmlDoc = new XmlDocument();
                  xmlDoc.LoadXml( "<parentnode><s econdchildnode> <b/></
                  secondchildnode ></parentnode>");
                  XmlNodeList xnl = xmlDoc.SelectNo des("//*[not(node())]");
                  Debug.Assert(xn l.Count == 1);

                  or you could remove it from the object model after its been loaded
                  (deserialized).

                  If you were using Liquid XML then you may be able to add code in that
                  would remove the empty elements as part of the de-serialization.

                  Cheers Simon


                  On 31 Jul, 21:22, j.a. harriman <jeffrey_no_spa m_al...@nospam. nospam>
                  wrote:
                  Simon,
                  >
                  Thanks for the link, I will take a look at it as it may be something we
                  could use.
                  >
                  In the meantime, I did find a code snippet that removes the "empty" nodes
                  but it removes ALL the empty nodes in the XML and I didn't state (earlier)
                  that I want to do this beginning with a particular child node only.
                  >
                  When I use the following XPath statement in XMLSpy's XPath Evaluator, it
                  locates the "empty" node mentioned in the previous posts:
                  /parentnode/secondchildnode//*[not(node())]
                  >
                  When I use the same statement in my C# code, it will not locate any empty
                  nodes "under" the "secondchildnod e" (count = 0):
                  XmlNodeList element =
                  doc.SelectNodes ("/parentnode/secondchildnode//*[not(node())]");
                  >
                  The plan is to loop through "element" and remove the nodes using the
                  "ParentNode.Rem oveChild" of an XmlNode - setting it to each array element,
                  etc.
                  >
                  Any ideas as to why this is not working in .NET VS2005 2.0 framework?
                  >
                  Thanks.
                  >
                  Jeff
                  >
                  "Sprotty" wrote:
                  Hi Jeff
                  >
                  The code produced by xsd.exe, copes well with simple schema's, but if
                  your working against anything a bit complicated then it can cause
                  issues.
                  >
                  If you continue to have problems then I suggest you take a look at
                  Liquid XML Data Binder
                  >
                  Liquid XML Data Binder is an advanced code generator allowing you to serialize, validate and edit XML documents via a strongly typed API in C#, C++, Java, VB .Net and Visual Basic 6 (COM). Features include validation against the W3C XSD standard, Sample Application generation, and HTML Documentation generation. Supports industry standard XSD schema such as DITA, DocBook, FpML, HL7, OAGIS, OASIS, OpenGIS, XBRL

                  >
                  This supports much more of the xsd standard, and the objects are more
                  strongly typed (it also comes with a free XSD editor).
                  >
                  Hope this helps
                  >
                  Regards Simon
                  >
                  On 23 Jul, 08:00, "Joe Fawcett" <joefawc...@new sgroup.nospamwr ote:
                  Jeff
                  >
                  IXmlSerializabl e is reasonably straightforward .
                  The newer version needs three methods, ReadXml, WriteXml and one to find the
                  Schema which is pointed to by an attribute, I normally just include the
                  schema as an embedded resource.
                  In ReadXml you get an XmlReader containing the XML and use it to populate
                  the object's fields, either reading it directly or loading it into a
                  DomDocumnt/XPathDocument if that's easier. WriteXml takes the fields and
                  creates an XML document.
                  There is an example here:http://www.devx.com/dotnet/Article/29720
                  >
                  --
                  >
                  Joe Fawcett (MVP - XML)http://joe.fawcett.name
                  >
                  "j.a. harriman" <jeffrey_no_spa m_al...@nospam. nospamwrote in message
                  >
                  >news:046D499 3-D7DE-405A-814C-F5B86AC29005@mi crosoft.com...
                  >
                  Thanks for the answer Joe.
                  >
                  I also found some examples of using a style sheet and doing a
                  transformation
                  (Using XslCompiledTran sform) of the original XmlDocument. I've run a test
                  and it seems to work in that the result is as if the original message
                  hadn't
                  had them in there. Are there any reasons why not to implement this?
                  >
                  Also, I tried to locate good "beginner" examples of using IXmlSerializabl e
                  that might be similar to what I need to do, but came up short. I would
                  like
                  to to look at this in further detail.
                  >
                  Do you have any links to examples or are you aware if any of the Microsoft
                  example downloads (such as SDK) have any?
                  >
                  Thanks. Jeff
                  >
                  "Joe Fawcett" wrote:
                  >
                  Unless you want to implement IXmlSerializabl e then I don't think you can
                  do
                  what you want. You can specify that a null element is shown as:
                  <myElement xsi:nil="true"/>
                  rather than being omitted entirely by adding the XmlElement(IsNu llable =
                  true) attribute to the field/property.
                  >
                  --
                  >
                  Joe Fawcett (MVP - XML)
                  >
                  >http://joe.fawcett.name
                  >
                  "j.a. harriman" <jeffrey_no_spa m_al...@nospam. nospamwrote in message
                  >news:697F7C6 1-82AC-4387-8F04-57D200C7C703@mi crosoft.com...
                  Hi,
                  >
                  I have a schema that has an optional element, fieldTag4000Fie ld. If
                  the
                  element is omitted from the XML request, when it is deserialized, it
                  will
                  be
                  null when I check it - which is fine.
                  >
                  What happens when the element is supplied as <fieldTag4000Fi eld/>
                  (empty),
                  it does not equate to null. I want to be able handle this at the
                  deserialization level rahter than in my edits later.
                  >
                  Is there a way to alter the behavior so when I deserialize it, it is
                  null?
                  >
                  I also want to add that I am calling the "CanDeserialize " method of the
                  XmlSerializer object, passing in the request as a XmlNodeReader.
                  >
                  Thanks.
                  >
                  The following C# class snippet was generated by xsd.exe V2:
                  >
                  [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
                  "2.0.50727. 42")]
                  [System.Serializ ableAttribute()]
                  [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
                  [System.Componen tModel.Designer CategoryAttribu te("code")]
                  [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
                  Namespace="MyNa mespace")]
                  public partial class Request_TypeMes sageType {
                  >
                  private Request_TypeMes sageTypeFieldTa g4000 fieldTag4000Fie ld;
                  >
                  public Request_TypeMes sageTypeFieldTa g4000 FieldTag4000 {
                  get {
                  return this.fieldTag40 00Field;
                  }
                  set {
                  this.fieldTag40 00Field = value;
                  }
                  }
                  }
                  >
                  [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
                  "2.0.50727. 42")]
                  [System.Serializ ableAttribute()]
                  [System.Diagnost ics.DebuggerSte pThroughAttribu te()]
                  [System.Componen tModel.Designer CategoryAttribu te("code")]
                  [System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType=true ,
                  Namespace="MyNa mespace")]
                  public partial class Request_TypeMes sageTypeFieldTa g4000 {
                  >
                  private IDType_Type intermediaryIDC odeField;
                  >
                  private bool intermediaryIDC odeFieldSpecifi ed;
                  >
                  private string intermediaryIde ntifierField;
                  >
                  public IDType_Type IntermediaryIDC ode {
                  get {
                  return this.intermedia ryIDCodeField;
                  }
                  set {
                  this.intermedia ryIDCodeField = value;
                  }
                  }
                  >
                  [System.Xml.Seri alization.XmlIg noreAttribute()]
                  public bool IntermediaryIDC odeSpecified {
                  get {
                  return this.intermedia ryIDCodeFieldSp ecified;
                  }
                  set {
                  this.intermedia ryIDCodeFieldSp ecified = value;
                  }
                  }
                  >
                  public string IntermediaryIde ntifier {
                  get {
                  return this.intermedia ryIdentifierFie ld;
                  }
                  set {
                  this.intermedia ryIdentifierFie ld = value;
                  }
                  }
                  }
                  >
                  [System.CodeDom. Compiler.Genera tedCodeAttribut e("xsd",
                  "2.0.50727. 42")]
                  [System.Serializ ableAttribute()]
                  [System.Xml.Seri alization.XmlTy peAttribute(Nam espace="MyNames pace")]
                  public enum IDType_Type {
                  >
                  B,
                  >
                  C,
                  >
                  D,
                  >
                  F,
                  >
                  U,
                  }- Hide quoted text -
                  >
                  - Show quoted text -

                  Comment

                  • Martin Honnen

                    #10
                    Re: XmlSerializer: deserialize against xsd generated class

                    j.a. harriman wrote:
                    The "real" XML has a number of "xmlns:" entries following the "parentnode ".
                    When I remove them & test, this statement,
                    "/parentnode/secondchildnode//*[not(node())]", works as I was expecting.
                    >
                    Is there something I need to do with adding namespaces (e.g
                    XmlNamespaceMan ager) or something else that I am unaware of?
                    If there are XML namespace declarations and you have elements or
                    attributes in those namespaces you want to select with XPath then you
                    indeed should use an XmlNamespaceMan ager and bind prefixes to the
                    namespace URIs to use the prefixes in your XPath expressions to qualify
                    element or attribute names. The XmlNamespaceMan ager is then passed as
                    the second argument to the SelectNodes or SelectSingleNod e calls.
                    If you need more concrete help then show us the relevant XML.

                    --

                    Martin Honnen --- MVP XML

                    Comment

                    Working...