deserializing xml

F

farseer

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.XmlSerializationReaderPDocument.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.ExecuteCustomizationStartupCode()
at
Microsoft.VisualStudio.Tools.Applications.Runtime.AppDomainManagerInternal.ExecuteCustomization(IHostServiceProvider
serviceProvider)
 
O

Ollie Riches

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
 
F

farseer

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.XmlSerializationReaderPDocument.Read13_PDocument(Boolean
isNullable, Boolean checkType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderPDocument.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.ExecuteCustomizationStartupCode()
at
Microsoft.VisualStudio.Tools.Applications.Runtime.AppDomainManagerInternal.ExecuteCustomization(IHostServiceProvider
serviceProvider)


************** Loaded Assemblies **************
 
G

Guest

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.
 
F

farseer

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.
 
G

Guest

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
 
F

farseer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top