469,609 Members | 1,657 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

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 programmactically 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.InvalidOperationException: There is an error in XML document (1,
40). ---> System.InvalidOperationException: <PDocument xmlns=''> was
not expected.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationReaderPDocument.Read14_PDocument()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader
xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader
xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader
xmlReader)
...
...{it goes on to show the stack trace here....
...
at Microsoft.Office.Tools.Word.Document.OnStartup()
at ThisDocument.FinishInitialization() in
C:\Proj\Test\MyNote.Designer.vb:line 83
at
Microsoft.VisualStudio.Tools.Applications.Runtime. AppDomainManagerInternal.ExecutePhase(String
methodName)
at
Microsoft.VisualStudio.Tools.Applications.Runtime. AppDomainManagerInternal.ExecuteCustomizationStart upCode()
at
Microsoft.VisualStudio.Tools.Applications.Runtime. AppDomainManagerInternal.ExecuteCustomization(IHos tServiceProvider
serviceProvider)

Nov 17 '05 #1
7 15402
And what do the classes for the xml serialisation\deserialisation look like?
Are you generating these or are they provided with the xml as well?

HTH

Ollie Riches

"farseer" <fa*****@optonline.net> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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 programmactically 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.InvalidOperationException: There is an error in XML document (1,
40). ---> System.InvalidOperationException: <PDocument xmlns=''> was
not expected.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationReaderPDocument.Read14_PDocument()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader
xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader
xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader
xmlReader)
...
...{it goes on to show the stack trace here....
...
at Microsoft.Office.Tools.Word.Document.OnStartup()
at ThisDocument.FinishInitialization() in
C:\Proj\Test\MyNote.Designer.vb:line 83
at
Microsoft.VisualStudio.Tools.Applications.Runtime. AppDomainManagerInternal.ExecutePhase(String
methodName)
at
Microsoft.VisualStudio.Tools.Applications.Runtime. AppDomainManagerInternal.ExecuteCustomizationStart upCode()
at
Microsoft.VisualStudio.Tools.Applications.Runtime. AppDomainManagerInternal.ExecuteCustomization(IHos tServiceProvider
serviceProvider)

Nov 17 '05 #2
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="mynamespace">
<para>this is a para
<w:bold>xmlns:w="someothernamespaceidon'tcareabout ">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 XmlAttributeOverrides
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.InvalidOperationException: There is an error in XML document (1,
585). ---> System.Xml.XmlException: Unexpected node type Element.
ReadElementString method can only be called on elements with simple or
empty content. Line 1, position 585.
at System.Xml.XmlReader.ReadElementString()
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationReaderPDocument.Read13_PDocument(Boole an
isNullable, Boolean checkType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationReaderPDocument.Read14_PDocument()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader
xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader
xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader
xmlReader)
...
at Microsoft.Office.Tools.Word.Document.OnStartup()
at ThisDocument.FinishInitialization() in
C:\Proj\Test\MyNote.Designer.vb:line 83
at
Microsoft.VisualStudio.Tools.Applications.Runtime. AppDomainManagerInternal.ExecutePhase(String
methodName)
at
Microsoft.VisualStudio.Tools.Applications.Runtime. AppDomainManagerInternal.ExecuteCustomizationStart upCode()
at
Microsoft.VisualStudio.Tools.Applications.Runtime. AppDomainManagerInternal.ExecuteCustomization(IHos tServiceProvider
serviceProvider)
************** Loaded Assemblies **************

Nov 17 '05 #3
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
www.mwtech.com

Nov 17 '05 #4
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 "SelectSingleNode" 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.

Nov 17 '05 #5
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
www.mwtech.com

"farseer" wrote:
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 "SelectSingleNode" 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.

Nov 17 '05 #6
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:

http://msdn.microsoft.com/library/de...classtopic.asp

HTH

Ollie Riches

"farseer" <fa*****@optonline.net> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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 "SelectSingleNode" 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.

Nov 17 '05 #7
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.insertXML 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.XmlNode and i don't see any easy way of adding an xml string
OR another node to easily.

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Justin Armstrong | last post: by
4 posts views Thread by Wayne Wengert | last post: by
6 posts views Thread by Steve Teeples | last post: by
1 post views Thread by =?Utf-8?B?SmVyZW15X0I=?= | last post: by
reply views Thread by Solution2021 | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.