Serialize XML is broken?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy B

    Serialize XML is broken?

    I have the following code in a default.aspx web form page_load event. There
    seems to be a problem with line 5 (NewsArticle.Da te = line).

    //create a news article

    NewsArticle NewsArticle = new NewsArticle();

    NewsArticle.Bod y = "This is a test news article...";

    NewsArticle.Tit le = "Testing XML";

    NewsArticle.Dat e = Convert.ToDateT ime("4/3/2008");

    NewsArticle.ID = "1";

    //serialize the NewsArticle into an xml document

    XmlDocument NewsArticleXml = new XmlDocument();

    XmlSerializer Serializer = new XmlSerializer(N ewsArticle.GetT ype());

    StringBuilder SB = new StringBuilder() ;

    StringWriter Writer = new StringWriter(SB );

    Serializer.Seri alize(Writer, NewsArticle);

    NewsArticleXml. LoadXml(SB.ToSt ring());

    Response.Clear( );

    Response.Conten tType = "text/xml";

    NewsArticleXml. Save(Response.O utput);



    The result of the last line above in a web browser is this: Any idea where
    the date attribute went? The NewsArticle object above was converted with xsd
    from a schema to a class.

    <?xml version="1.0" encoding="utf-8" ?>
    - <NewsArticle xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http ://www.w3.org/2001/XMLSchema" ID="1" Title="Testing XML">
    <Body>This is a test news article...</Body>
    </NewsArticle>


  • Martin Honnen

    #2
    Re: Serialize XML is broken?

    Andy B wrote:
    I have the following code in a default.aspx web form page_load event. There
    seems to be a problem with line 5 (NewsArticle.Da te = line).
    >
    //create a news article
    >
    NewsArticle NewsArticle = new NewsArticle();
    >
    NewsArticle.Bod y = "This is a test news article...";
    >
    NewsArticle.Tit le = "Testing XML";
    >
    NewsArticle.Dat e = Convert.ToDateT ime("4/3/2008");
    >
    NewsArticle.ID = "1";
    >
    //serialize the NewsArticle into an xml document
    >
    XmlDocument NewsArticleXml = new XmlDocument();
    >
    XmlSerializer Serializer = new XmlSerializer(N ewsArticle.GetT ype());
    >
    StringBuilder SB = new StringBuilder() ;
    >
    StringWriter Writer = new StringWriter(SB );
    >
    Serializer.Seri alize(Writer, NewsArticle);
    >
    NewsArticleXml. LoadXml(SB.ToSt ring());
    >
    Response.Clear( );
    >
    Response.Conten tType = "text/xml";
    >
    NewsArticleXml. Save(Response.O utput);
    I don't understand why you need the XmlDocument, you should simply let
    the Serialize method write to Response.Output , no need for StringWriter
    and XmlDocument.

    The result of the last line above in a web browser is this: Any idea where
    the date attribute went? The NewsArticle object above was converted with xsd
    from a schema to a class.
    >
    <?xml version="1.0" encoding="utf-8" ?>
    - <NewsArticle xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http ://www.w3.org/2001/XMLSchema" ID="1" Title="Testing XML">
    <Body>This is a test news article...</Body>
    </NewsArticle>
    Can you post the schema and the .NET code for NewsArticle?


    --

    Martin Honnen --- MVP XML

    Comment

    • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

      #3
      RE: Serialize XML is broken?

      You must be doing some custom serialization to get some properties as
      attributes (id, title) and some as elements (body). The problem likely is in
      your NewsArticle class.

      "Andy B" wrote:
      I have the following code in a default.aspx web form page_load event. There
      seems to be a problem with line 5 (NewsArticle.Da te = line).
      >
      //create a news article
      >
      NewsArticle NewsArticle = new NewsArticle();
      >
      NewsArticle.Bod y = "This is a test news article...";
      >
      NewsArticle.Tit le = "Testing XML";
      >
      NewsArticle.Dat e = Convert.ToDateT ime("4/3/2008");
      >
      NewsArticle.ID = "1";
      >
      //serialize the NewsArticle into an xml document
      >
      XmlDocument NewsArticleXml = new XmlDocument();
      >
      XmlSerializer Serializer = new XmlSerializer(N ewsArticle.GetT ype());
      >
      StringBuilder SB = new StringBuilder() ;
      >
      StringWriter Writer = new StringWriter(SB );
      >
      Serializer.Seri alize(Writer, NewsArticle);
      >
      NewsArticleXml. LoadXml(SB.ToSt ring());
      >
      Response.Clear( );
      >
      Response.Conten tType = "text/xml";
      >
      NewsArticleXml. Save(Response.O utput);
      >
      >
      >
      The result of the last line above in a web browser is this: Any idea where
      the date attribute went? The NewsArticle object above was converted with xsd
      from a schema to a class.
      >
      <?xml version="1.0" encoding="utf-8" ?>
      - <NewsArticle xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
      xmlns:xsd="http ://www.w3.org/2001/XMLSchema" ID="1" Title="Testing XML">
      <Body>This is a test news article...</Body>
      </NewsArticle>
      >
      >
      >

      Comment

      • Andy B

        #4
        Re: Serialize XML is broken?

        Hi...

        The XmlDocument was supposed to be created to make it easier to insert into
        a database as an xml field. Is there any better way of doing this than what
        was listed? I suppose I could just serialize to a string and insert that
        into the database. Then load it as xml when it is needed. Anyways, here is
        the schema and .net class for NewsArticle:

        <?xml version="1.0" encoding="utf-8"?>

        <xs:schema id="NewsArticle "

        targetNamespace ="http://tempuri.org/NewsArticle.xsd "

        elementFormDefa ult="qualified"

        xmlns="http://tempuri.org/NewsArticle.xsd "

        xmlns:mstns="ht tp://tempuri.org/NewsArticle.xsd "

        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        >
        <xs:element name="NewsArtic le">

        <xs:complexType >

        <xs:sequence>

        <xs:element name="Body" type="xs:string " />

        </xs:sequence>

        <xs:attribute name="ID" type="xs:positi veInteger" />

        <xs:attribute name="Title" type="xs:string " />

        <xs:attribute name="Date" type="xs:dateTi me" />

        </xs:complexType>

        </xs:element>

        </xs:schema>

        using System.Xml.Seri alization;

        [System.Serializ ableAttribute()]

        [System.Xml.Seri alization.XmlRo otAttribute(IsN ullable=true)]

        public partial class NewsArticle {


        private string bodyField;

        private string idField;

        private string titleField;

        private System.DateTime dateField;

        private bool dateFieldSpecif ied;


        public string Body {

        get {

        return this.bodyField;

        }

        set {

        this.bodyField = value;

        }

        }


        [System.Xml.Seri alization.XmlAt tributeAttribut e(DataType="pos itiveInteger")]

        public string ID {

        get {

        return this.idField;

        }

        set {

        this.idField = value;

        }

        }


        [System.Xml.Seri alization.XmlAt tributeAttribut e()]

        public string Title {

        get {

        return this.titleField ;

        }

        set {

        this.titleField = value;

        }

        }


        [System.Xml.Seri alization.XmlAt tributeAttribut e(DataType="Dat eTime")]

        public System.DateTime Date {

        get {

        return this.dateField;

        }

        set {

        this.dateField = value;

        }

        }


        [System.Xml.Seri alization.XmlIg noreAttribute()]

        public bool DateSpecified {

        get {

        return this.dateFieldS pecified;

        }

        set {

        this.dateFieldS pecified = value;

        }

        }

        }

        "Martin Honnen" <mahotrash@yaho o.dewrote in message
        news:euPFcxKoIH A.3532@TK2MSFTN GP03.phx.gbl...
        Andy B wrote:
        >I have the following code in a default.aspx web form page_load event.
        >There seems to be a problem with line 5 (NewsArticle.Da te = line).
        >>
        >//create a news article
        >>
        >NewsArticle NewsArticle = new NewsArticle();
        >>
        >NewsArticle.Bo dy = "This is a test news article...";
        >>
        >NewsArticle.Ti tle = "Testing XML";
        >>
        >NewsArticle.Da te = Convert.ToDateT ime("4/3/2008");
        >>
        >NewsArticle. ID = "1";
        >>
        >//serialize the NewsArticle into an xml document
        >>
        >XmlDocument NewsArticleXml = new XmlDocument();
        >>
        >XmlSerialize r Serializer = new XmlSerializer(N ewsArticle.GetT ype());
        >>
        >StringBuilde r SB = new StringBuilder() ;
        >>
        >StringWriter Writer = new StringWriter(SB );
        >>
        >Serializer.Ser ialize(Writer, NewsArticle);
        >>
        >NewsArticleXml .LoadXml(SB.ToS tring());
        >>
        >Response.Clear ();
        >>
        >Response.Conte ntType = "text/xml";
        >>
        >NewsArticleXml .Save(Response. Output);
        >
        I don't understand why you need the XmlDocument, you should simply let the
        Serialize method write to Response.Output , no need for StringWriter and
        XmlDocument.
        >
        >
        >The result of the last line above in a web browser is this: Any idea
        >where the date attribute went? The NewsArticle object above was converted
        >with xsd from a schema to a class.
        >>
        > <?xml version="1.0" encoding="utf-8" ?>
        >- <NewsArticle xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
        >xmlns:xsd="htt p://www.w3.org/2001/XMLSchema" ID="1" Title="Testing XML">
        > <Body>This is a test news article...</Body>
        > </NewsArticle>
        >
        Can you post the schema and the .NET code for NewsArticle?
        >
        >
        --
        >
        Martin Honnen --- MVP XML
        http://JavaScript.FAQTs.com/

        Comment

        • Martin Honnen

          #5
          Re: Serialize XML is broken?

          Andy B wrote:
          [System.Serializ ableAttribute()]
          >
          [System.Xml.Seri alization.XmlRo otAttribute(IsN ullable=true)]
          >
          public partial class NewsArticle {
          >
          >
          private string bodyField;
          >
          private string idField;
          >
          private string titleField;
          >
          private System.DateTime dateField;
          >
          private bool dateFieldSpecif ied;
          >
          >
          public string Body {
          >
          get {
          >
          return this.bodyField;
          >
          }
          >
          set {
          >
          this.bodyField = value;
          >
          }
          >
          }
          >
          >
          [System.Xml.Seri alization.XmlAt tributeAttribut e(DataType="pos itiveInteger")]
          >
          public string ID {
          >
          get {
          >
          return this.idField;
          >
          }
          >
          set {
          >
          this.idField = value;
          >
          }
          >
          }
          >
          >
          [System.Xml.Seri alization.XmlAt tributeAttribut e()]
          >
          public string Title {
          >
          get {
          >
          return this.titleField ;
          >
          }
          >
          set {
          >
          this.titleField = value;
          >
          }
          >
          }
          >
          >
          [System.Xml.Seri alization.XmlAt tributeAttribut e(DataType="Dat eTime")]
          >
          public System.DateTime Date {
          >
          get {
          >
          return this.dateField;
          >
          }
          >
          set {
          >
          this.dateField = value;
          >
          }
          >
          }
          >
          >
          [System.Xml.Seri alization.XmlIg noreAttribute()]
          >
          public bool DateSpecified {
          >
          get {
          >
          return this.dateFieldS pecified;
          >
          }
          >
          set {
          >
          this.dateFieldS pecified = value;
          >
          }
          With that class definition you need to set

          NewsArticle.Dat eSpecified = true;

          to have the Date field show up.

          --

          Martin Honnen --- MVP XML

          Comment

          • Chris Shepherd

            #6
            Re: Serialize XML is broken?

            Family Tree Mike wrote:
            You must be doing some custom serialization to get some properties as
            attributes (id, title) and some as elements (body). The problem likely is in
            your NewsArticle class.
            Applying attributes to the properties isn't really custom serialization is it?

            You can slap [XmlElement("pro perty1")] before a property, and have it serialize
            as an actual element, and [XmlAttribute("p roperty2")] to have it serialize as an
            attribute.

            I'm not being critical here, just curious if that's considered custom
            serialization. To my mind the point at which you need to write your own actual
            serialization logic, rather than let .NET handle it, is the point at which it's
            now custom serialization.

            Chris.

            Comment

            • Andy B

              #7
              Re: Serialize XML is broken?

              Sounds like this might be more like custom xml document formatting more than
              custom serialization.. ..


              "Chris Shepherd" <chsh@nospam.ch sh.cawrote in message
              news:ushFAcLoIH A.3556@TK2MSFTN GP04.phx.gbl...
              Family Tree Mike wrote:
              >You must be doing some custom serialization to get some properties as
              >attributes (id, title) and some as elements (body). The problem likely
              >is in your NewsArticle class.
              >
              Applying attributes to the properties isn't really custom serialization is
              it?
              >
              You can slap [XmlElement("pro perty1")] before a property, and have it
              serialize as an actual element, and [XmlAttribute("p roperty2")] to have it
              serialize as an attribute.
              >
              I'm not being critical here, just curious if that's considered custom
              serialization. To my mind the point at which you need to write your own
              actual serialization logic, rather than let .NET handle it, is the point
              at which it's now custom serialization.
              >
              Chris.

              Comment

              • Andy B

                #8
                Re: Serialize XML is broken?

                Ok setting NewsArticle.Dat eSpecified = true; did the job... tnx for the
                help...


                "Martin Honnen" <mahotrash@yaho o.dewrote in message
                news:exFutSLoIH A.2188@TK2MSFTN GP04.phx.gbl...
                Andy B wrote:
                >
                >[System.Serializ ableAttribute()]
                >>
                >[System.Xml.Seri alization.XmlRo otAttribute(IsN ullable=true)]
                >>
                >public partial class NewsArticle {
                >>
                >>
                >private string bodyField;
                >>
                >private string idField;
                >>
                >private string titleField;
                >>
                >private System.DateTime dateField;
                >>
                >private bool dateFieldSpecif ied;
                >>
                >>
                >public string Body {
                >>
                >get {
                >>
                >return this.bodyField;
                >>
                >}
                >>
                >set {
                >>
                >this.bodyFie ld = value;
                >>
                >}
                >>
                >}
                >>
                >>
                >[System.Xml.Seri alization.XmlAt tributeAttribut e(DataType="pos itiveInteger")]
                >>
                >public string ID {
                >>
                >get {
                >>
                >return this.idField;
                >>
                >}
                >>
                >set {
                >>
                >this.idField = value;
                >>
                >}
                >>
                >}
                >>
                >>
                >[System.Xml.Seri alization.XmlAt tributeAttribut e()]
                >>
                >public string Title {
                >>
                >get {
                >>
                >return this.titleField ;
                >>
                >}
                >>
                >set {
                >>
                >this.titleFiel d = value;
                >>
                >}
                >>
                >}
                >>
                >>
                >[System.Xml.Seri alization.XmlAt tributeAttribut e(DataType="Dat eTime")]
                >>
                >public System.DateTime Date {
                >>
                >get {
                >>
                >return this.dateField;
                >>
                >}
                >>
                >set {
                >>
                >this.dateFie ld = value;
                >>
                >}
                >>
                >}
                >>
                >>
                >[System.Xml.Seri alization.XmlIg noreAttribute()]
                >>
                >public bool DateSpecified {
                >>
                >get {
                >>
                >return this.dateFieldS pecified;
                >>
                >}
                >>
                >set {
                >>
                >this.dateField Specified = value;
                >>
                >}
                >
                With that class definition you need to set
                >
                NewsArticle.Dat eSpecified = true;
                >
                to have the Date field show up.
                >
                --
                >
                Martin Honnen --- MVP XML
                http://JavaScript.FAQTs.com/

                Comment

                • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

                  #9
                  Re: Serialize XML is broken?

                  Yes, that is what I should have said. What I meant was custom relative to
                  what the class would be without any attributes on the properties. That could
                  be done in a variety of ways, not having seen the code at that point.

                  "Andy B" wrote:
                  Sounds like this might be more like custom xml document formatting more than
                  custom serialization.. ..
                  >
                  >
                  "Chris Shepherd" <chsh@nospam.ch sh.cawrote in message
                  news:ushFAcLoIH A.3556@TK2MSFTN GP04.phx.gbl...
                  Family Tree Mike wrote:
                  You must be doing some custom serialization to get some properties as
                  attributes (id, title) and some as elements (body). The problem likely
                  is in your NewsArticle class.
                  Applying attributes to the properties isn't really custom serialization is
                  it?

                  You can slap [XmlElement("pro perty1")] before a property, and have it
                  serialize as an actual element, and [XmlAttribute("p roperty2")] to have it
                  serialize as an attribute.

                  I'm not being critical here, just curious if that's considered custom
                  serialization. To my mind the point at which you need to write your own
                  actual serialization logic, rather than let .NET handle it, is the point
                  at which it's now custom serialization.

                  Chris.
                  >
                  >
                  >

                  Comment

                  Working...