deserializing xml

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

    deserializing xml

    Hi,
    i am getting an error when trying to deserialize and xml. the error is
    shown below. i get this error regardless if i try to desearialize in
    C# or VB. the XML itself does infact contain a top level "PDocument"
    note which contains an xmlns namespace and some other attriute. is the
    error belowing telling it doesn't like that namespace declaration? how
    can i programmactical ly fix this, as i do not have control of the xml i
    recieve...i just know that they are valid.

    ---------------------------------------------
    There is an error in XML document (1, 40).


    ************** Exception Text **************
    System.InvalidO perationExcepti on: There is an error in XML document (1,
    40). ---> System.InvalidO perationExcepti on: <PDocument xmlns=''> was
    not expected.
    at
    Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializati onReaderPDocume nt.Read14_PDocu ment()
    --- End of inner exception stack trace ---
    at System.Xml.Seri alization.XmlSe rializer.Deseri alize(XmlReader
    xmlReader, String encodingStyle, XmlDeserializat ionEvents events)
    at System.Xml.Seri alization.XmlSe rializer.Deseri alize(XmlReader
    xmlReader, String encodingStyle)
    at System.Xml.Seri alization.XmlSe rializer.Deseri alize(XmlReader
    xmlReader)
    ...
    ...{it goes on to show the stack trace here....
    ...
    at Microsoft.Offic e.Tools.Word.Do cument.OnStartu p()
    at ThisDocument.Fi nishInitializat ion() in
    C:\Proj\Test\My Note.Designer.v b:line 83
    at
    Microsoft.Visua lStudio.Tools.A pplications.Run time.AppDomainM anagerInternal. ExecutePhase(St ring
    methodName)
    at
    Microsoft.Visua lStudio.Tools.A pplications.Run time.AppDomainM anagerInternal. ExecuteCustomiz ationStartupCod e()
    at
    Microsoft.Visua lStudio.Tools.A pplications.Run time.AppDomainM anagerInternal. ExecuteCustomiz ation(IHostServ iceProvider
    serviceProvider )

  • Ollie Riches

    #2
    Re: deserializing xml

    And what do the classes for the xml serialisation\d eserialisation look like?
    Are you generating these or are they provided with the xml as well?

    HTH

    Ollie Riches

    "farseer" <farseer@optonl ine.net> wrote in message
    news:1129693150 .775269.274010@ o13g2000cwo.goo glegroups.com.. .[color=blue]
    > Hi,
    > i am getting an error when trying to deserialize and xml. the error is
    > shown below. i get this error regardless if i try to desearialize in
    > C# or VB. the XML itself does infact contain a top level "PDocument"
    > note which contains an xmlns namespace and some other attriute. is the
    > error belowing telling it doesn't like that namespace declaration? how
    > can i programmactical ly fix this, as i do not have control of the xml i
    > recieve...i just know that they are valid.
    >
    > ---------------------------------------------
    > There is an error in XML document (1, 40).
    >
    >
    > ************** Exception Text **************
    > System.InvalidO perationExcepti on: There is an error in XML document (1,
    > 40). ---> System.InvalidO perationExcepti on: <PDocument xmlns=''> was
    > not expected.
    > at
    > Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializati onReaderPDocume nt.Read14_PDocu ment()
    > --- End of inner exception stack trace ---
    > at System.Xml.Seri alization.XmlSe rializer.Deseri alize(XmlReader
    > xmlReader, String encodingStyle, XmlDeserializat ionEvents events)
    > at System.Xml.Seri alization.XmlSe rializer.Deseri alize(XmlReader
    > xmlReader, String encodingStyle)
    > at System.Xml.Seri alization.XmlSe rializer.Deseri alize(XmlReader
    > xmlReader)
    > ...
    > ...{it goes on to show the stack trace here....
    > ...
    > at Microsoft.Offic e.Tools.Word.Do cument.OnStartu p()
    > at ThisDocument.Fi nishInitializat ion() in
    > C:\Proj\Test\My Note.Designer.v b:line 83
    > at
    > Microsoft.Visua lStudio.Tools.A pplications.Run time.AppDomainM anagerInternal. ExecutePhase(St ring
    > methodName)
    > at
    > Microsoft.Visua lStudio.Tools.A pplications.Run time.AppDomainM anagerInternal. ExecuteCustomiz ationStartupCod e()
    > at
    > Microsoft.Visua lStudio.Tools.A pplications.Run time.AppDomainM anagerInternal. ExecuteCustomiz ation(IHostServ iceProvider
    > serviceProvider )
    >[/color]


    Comment

    • farseer

      #3
      Re: deserializing xml

      I am recieving a XML that contains several namespaces, including
      elements belonging to my namespace. i transform the xml to in a new
      element that conforms to our schema (filter out other elements i don't
      care about). our schema supports mixed content for certain
      elements..this means it can allow elements that are not necessarily
      part of our namespace. in those cases, the name space is an attribute
      of that particular element itself. so after transformation, it might
      look liks something like this:

      <document xmlns:myns="myn amespace">
      <para>this is a para
      <w:bold>xmlns:w ="someothername spaceidon'tcare about">with
      stuff</w:bold>i dont carea bout</para>
      ....
      </document>

      so you see, the top level element declares the name space i know about,
      but in cases where i want to preserve everything within a "para"
      elelement, i simply take it all, and the transformation (be default)
      will copy namespaces of elements not defined in my schema. in the
      above example para is defined as "mixed" in our schema. this all works
      nicely.

      I was able the solve the first issue i originally posted about by
      adding this code prior to deserializing the xml:
      Dim xmlattr As New XmlAttributes
      Dim attroveride As New XmlAttributeOve rrides
      attroveride.Add ( GetType(MyDoc), xmlattr )
      Dim serializer As XmlSerializer = New XmlSerializer(
      GetType(MyDoc), attroveride )

      i am getting further, but now i am getting another error shown below.
      it appears that is referring to this namespace in the xml
      "ns0:ActionPane ". this is something MS Word puts into xml if i am
      using an action pane and save the document as xml. is there away to
      filter out namespaces?

      ------------------------------------------------------------------
      There is an error in XML document (1, 585).


      ************** Exception Text **************
      System.InvalidO perationExcepti on: There is an error in XML document (1,
      585). ---> System.Xml.XmlE xception: Unexpected node type Element.
      ReadElementStri ng method can only be called on elements with simple or
      empty content. Line 1, position 585.
      at System.Xml.XmlR eader.ReadEleme ntString()
      at
      Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializati onReaderPDocume nt.Read13_PDocu ment(Boolean
      isNullable, Boolean checkType)
      at
      Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializati onReaderPDocume nt.Read14_PDocu ment()
      --- End of inner exception stack trace ---
      at System.Xml.Seri alization.XmlSe rializer.Deseri alize(XmlReader
      xmlReader, String encodingStyle, XmlDeserializat ionEvents events)
      at System.Xml.Seri alization.XmlSe rializer.Deseri alize(XmlReader
      xmlReader, String encodingStyle)
      at System.Xml.Seri alization.XmlSe rializer.Deseri alize(XmlReader
      xmlReader)
      ...
      at Microsoft.Offic e.Tools.Word.Do cument.OnStartu p()
      at ThisDocument.Fi nishInitializat ion() in
      C:\Proj\Test\My Note.Designer.v b:line 83
      at
      Microsoft.Visua lStudio.Tools.A pplications.Run time.AppDomainM anagerInternal. ExecutePhase(St ring
      methodName)
      at
      Microsoft.Visua lStudio.Tools.A pplications.Run time.AppDomainM anagerInternal. ExecuteCustomiz ationStartupCod e()
      at
      Microsoft.Visua lStudio.Tools.A pplications.Run time.AppDomainM anagerInternal. ExecuteCustomiz ation(IHostServ iceProvider
      serviceProvider )


      ************** Loaded Assemblies **************

      Comment

      • Robert W.

        #4
        Re: deserializing xml

        I don't know if my experience with XML Deserialization will benefit you but
        here goes: I built my own object model, which included nested classes,
        collections, and properties - several levels deep. Then I tried using the
        built-in XML serialization methods. But they didn't work. I did some
        research and discovered that an object model has to have a very specific
        structure in order for these methods to work. So instead, I used Reflection
        and some recursive code (that I'm quite proud of btw) to effect my own XML
        Serialization engine.

        When it came to reading the XML back in, the ReadXML did work, placing the
        data into a dataset consisting of several tables. I then had to write my own
        code to take the data from the data tables and bring it into my object model.

        LOTS of work for both export and import but I couldn't find any other way to
        do it. I wonder if you're in a similar predicament, doubly so because you
        don't control the structure of the XML file. If even ReadXML won't work then
        you could write your own XML Import parser. I don't think it would be that
        difficult to do.

        --
        Robert W.
        Vancouver, BC


        Comment

        • farseer

          #5
          Re: deserializing xml

          Robert...funny you should share that experience. that is EXACTLY what
          i am doing now. using reflection to write the xml and using some
          standard "SelectSingleNo de" calls to read back into my object model.
          It's exactly the effort i am having to put into this that is leading me
          to look at object serialization and de-serialization in an attempt to
          solve the problem.

          when you were derializing, were you specifiying XMLAttribute and
          XMLElement for properties so that they are correctly serialized?
          i am hoping, once i get past the above issues, that that will work.
          other "properties " that i don't want serialized, i make methods
          instead, so that my refelection routine only serializes the Properties
          and not the methods.

          Comment

          • Robert W.

            #6
            Re: deserializing xml

            I don't recall methods getting serialized. The only XML tag I used (in some
            cases) was: [XMLIgnore]

            I don't mind sharing some of my code with you, if it would help. For
            reasons of spam, I'm not going to list my e-mail on here, but you can drop me
            a line via this page: http://mwtech.com/contact.shtml

            --
            Robert W.
            Vancouver, BC




            "farseer" wrote:
            [color=blue]
            > Robert...funny you should share that experience. that is EXACTLY what
            > i am doing now. using reflection to write the xml and using some
            > standard "SelectSingleNo de" calls to read back into my object model.
            > It's exactly the effort i am having to put into this that is leading me
            > to look at object serialization and de-serialization in an attempt to
            > solve the problem.
            >
            > when you were derializing, were you specifiying XMLAttribute and
            > XMLElement for properties so that they are correctly serialized?
            > i am hoping, once i get past the above issues, that that will work.
            > other "properties " that i don't want serialized, i make methods
            > instead, so that my refelection routine only serializes the Properties
            > and not the methods.
            >
            >[/color]

            Comment

            • Ollie Riches

              #7
              Re: deserializing xml

              this is exactly what ISerializable was defined for, you shouldn't have to
              use reflection directlry for thisjust implement the interface and you can
              control how your objects are serialized:



              HTH

              Ollie Riches

              "farseer" <farseer@optonl ine.net> wrote in message
              news:1129764576 .566540.62660@z 14g2000cwz.goog legroups.com...[color=blue]
              > Robert...funny you should share that experience. that is EXACTLY what
              > i am doing now. using reflection to write the xml and using some
              > standard "SelectSingleNo de" calls to read back into my object model.
              > It's exactly the effort i am having to put into this that is leading me
              > to look at object serialization and de-serialization in an attempt to
              > solve the problem.
              >
              > when you were derializing, were you specifiying XMLAttribute and
              > XMLElement for properties so that they are correctly serialized?
              > i am hoping, once i get past the above issues, that that will work.
              > other "properties " that i don't want serialized, i make methods
              > instead, so that my refelection routine only serializes the Properties
              > and not the methods.
              >[/color]


              Comment

              • farseer

                #8
                Re: deserializing xml

                Serializing works great..and if i preface the properties with the
                XmlAttribute where necessary, the serializer can discern between what
                should be attributes and elements.
                Rightn now i am attempting to serialize the object to a string and then
                use XmlNode.insertX ML to add it to a document.

                The problem is i am trying to do this for a Microsoft Office document.
                Apparently it's version of XmlNode which must be used is different from
                System.Xml.XmlN ode and i don't see any easy way of adding an xml string
                OR another node to easily.

                Comment

                Working...