473,287 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Problem serializing type System.Xml.XmlElement

I have written a little application that can grab part of a page from a
web site. I then want to take this result and be able to serialize it
so that it can be stored as XML. I am storing these "ResultNodes" as
System.Xml.XmlElements but when I try and serialize (serialise) my
result object I get the following exception:

"System.InvalidOperationException" - "There was an error generating the
XML document"
"The type System.Xml.XmlElement may not be used in this context."
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces, String
encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces, String
encodingStyle)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o)
at <My Source Code>

The XSD for my ResultNodes collection is:
<xs:complexType name="ResultNodes">
<xs:sequence>
<xs:element name="ResultNode" type="xs:anyType" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

Can System.Xml.XmlElements be serialized? If not, what is the standard
solution for wrapping up nodes so that a result XML document may look
something like the following?

<?xml version="1.0"?>
<Root>
<Name>An example</Name>
<ResultNodes>
<!-- Grab the title element from a page -->
<ResultNode>
<title>Leggetter.co.uk</title>
</ResultNode>
<!-- Grab the body element from a page -->
<ResultNode>
<body>
<!-- An assortment of elements and things could be here
-->
</body>
</ResultNode>
<ResultNode>
<!-- Grab the entire page -->
<html>
<!-- ... -->
</html>
</ResultNode>
</ResultNodes>
</Root>
Many thanks

Jan 16 '07 #1
3 10407
Phil

I've not had a chance to experiment, but the first thing is HTML needs to be
escaped, or you need to contain the HTML is CDATA section.

You could try making the ResultNode type to xs:string.

HTH

Glenn
<ph**@leggetter.co.ukwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
I have written a little application that can grab part of a page from a
web site. I then want to take this result and be able to serialize it
so that it can be stored as XML. I am storing these "ResultNodes" as
System.Xml.XmlElements but when I try and serialize (serialise) my
result object I get the following exception:

"System.InvalidOperationException" - "There was an error generating the
XML document"
"The type System.Xml.XmlElement may not be used in this context."
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces, String
encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces, String
encodingStyle)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o)
at <My Source Code>

The XSD for my ResultNodes collection is:
<xs:complexType name="ResultNodes">
<xs:sequence>
<xs:element name="ResultNode" type="xs:anyType" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

Can System.Xml.XmlElements be serialized? If not, what is the standard
solution for wrapping up nodes so that a result XML document may look
something like the following?

<?xml version="1.0"?>
<Root>
<Name>An example</Name>
<ResultNodes>
<!-- Grab the title element from a page -->
<ResultNode>
<title>Leggetter.co.uk</title>
</ResultNode>
<!-- Grab the body element from a page -->
<ResultNode>
<body>
<!-- An assortment of elements and things could be here
-->
</body>
</ResultNode>
<ResultNode>
<!-- Grab the entire page -->
<html>
<!-- ... -->
</html>
</ResultNode>
</ResultNodes>
</Root>
Many thanks

Jan 16 '07 #2
Hi Glenn,

I'm actually converting all the HTML into XML/XHTML so that I can use
XPath to query the XHTML nodes. So, I thought that it would be
perfectly valid to put this converted HTML into the <ResultNode>?

Any ideas?

Phil

On Jan 16, 9:34 pm, "Glenn" <glenn.csh...@yahoo.co.ukwrote:
Phil

I've not had a chance to experiment, but the first thing is HTML needs to be
escaped, or you need to contain the HTML is CDATA section.

You could try making the ResultNode type to xs:string.

HTH

Glenn<p...@leggetter.co.ukwrote in messagenews:11**********************@a75g2000cwd.g ooglegroups.com...
I have written a little application that can grab part of a page from a
web site. I then want to take this result and be able to serialize it
so that it can be stored as XML. I am storing these "ResultNodes" as
System.Xml.XmlElements but when I try and serialize (serialise) my
result object I get the following exception:
"System.InvalidOperationException" - "There was an error generating the
XML document"
"The type System.Xml.XmlElement may not be used in this context."
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces, String
encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces, String
encodingStyle)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o)
at <My Source Code>
The XSD for my ResultNodes collection is:
<xs:complexType name="ResultNodes">
<xs:sequence>
<xs:element name="ResultNode" type="xs:anyType" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
Can System.Xml.XmlElements be serialized? If not, what is the standard
solution for wrapping up nodes so that a result XML document may look
something like the following?
<?xml version="1.0"?>
<Root>
<Name>An example</Name>
<ResultNodes>
<!-- Grab the title element from a page -->
<ResultNode>
<title>Leggetter.co.uk</title>
</ResultNode>
<!-- Grab the body element from a page -->
<ResultNode>
<body>
<!-- An assortment of elements and things could be here
-->
</body>
</ResultNode>
<ResultNode>
<!-- Grab the entire page -->
<html>
<!-- ... -->
</html>
</ResultNode>
</ResultNodes>
</Root>
Many thanks
Jan 30 '07 #3
On Jan 30, 1:40 pm, "phobos7" <p...@leggetter.co.ukwrote:
Hi Glenn,

I'm actually converting all the HTML into XML/XHTML so that I can use
XPath to query the XHTML nodes. So, I thought that it would be
perfectly valid to put this converted HTML into the <ResultNode>?

Any ideas?

Phil

On Jan 16, 9:34 pm, "Glenn" <glenn.csh...@yahoo.co.ukwrote:
Phil
I've not had a chance to experiment, but the first thing is HTML needs to be
escaped, or you need to contain the HTML is CDATA section.
You could try making the ResultNode type to xs:string.
HTH
Glenn<p...@leggetter.co.ukwrote in messagenews:11**********************@a75g2000cwd.g ooglegroups.com...
I have written a little application that can grab part of a page from a
web site. I then want to take this result and be able to serialize it
so that it can be stored as XML. I am storing these "ResultNodes" as
System.Xml.XmlElements but when I try and serialize (serialise) my
result object I get the following exception:
"System.InvalidOperationException" - "There was an error generating the
XML document"
"The type System.Xml.XmlElement may not be used in this context."
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces, String
encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces, String
encodingStyle)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o)
at <My Source Code>
The XSD for my ResultNodes collection is:
<xs:complexType name="ResultNodes">
<xs:sequence>
<xs:element name="ResultNode" type="xs:anyType" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
Can System.Xml.XmlElements be serialized? If not, what is the standard
solution for wrapping up nodes so that a result XML document may look
something like the following?
<?xml version="1.0"?>
<Root>
<Name>An example</Name>
<ResultNodes>
<!-- Grab the title element from a page -->
<ResultNode>
<title>Leggetter.co.uk</title>
</ResultNode>
<!-- Grab the body element from a page -->
<ResultNode>
<body>
<!-- An assortment of elements and things could be here
-->
</body>
</ResultNode>
<ResultNode>
<!-- Grab the entire page -->
<html>
<!-- ... -->
</html>
</ResultNode>
</ResultNodes>
</Root>
Many thanks
I eventually found two possible solutions to my problem:

1) Update the XSD to use <xs:any>:

<xs:complexType name="ResultNodes">
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

2) Once the C# class had been generated using XSDObjectGen.exe I would
then have to update the attribute applied to the generated
ResultNodeCollection:

//
[XmlElement(Type=typeof(object),ElementName="Result Node",IsNullable=false,Form=XmlSchemaForm.Qualifie d,Namespace=Declarations.SchemaVersion)]
[XmlAnyElement(Namespace=Declarations.SchemaVersion )]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public ResultNodeCollection __ResultNodeCollection;

I went for option 2 as it kept the very handy collection wrapper which
inherited from ArrayList in place. If I went for option 1
XSDObjectGen.exe did not create a collection wrapper class . It
instead created a simple array of XmlElements.

Mar 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Daniel Lidström | last post by:
Hi everyone. Let me describe what I am doing. I've created these classes: public class Vehicle { public Vehicle() {} public string Make; public string Model;
2
by: Werner B. Strydom | last post by:
Hi I wrote a class, Document, which has an Array of ITask objects. In order to serialize the tasks contained within the tasks array, I marked each task class with an attribute , and added an...
2
by: Aleksei Guzev | last post by:
Imagine one writing a class library CL1 for data storage. He defines classes ‘DataItem’ and ‘DataRecord’ so that the latter contains a collection of the former. And he derives class ‘IntItem’ from...
4
by: Wayne Wengert | last post by:
I am still stuck trying to create a Class to use for exporting and importing array data to/from XML. The format of the XML that I want to import/export is shown below as is the Class and the code I...
6
by: Ferrari, Eduardo | last post by:
Hi all! I'm trying to create this XML file: <?xml version="1.0" encoding="utf-8" ?> <Build type="Daily" sync="True" compile="True" assemble="True" > <Sync version="1.0.0.0" branch="QA">...
1
by: Jax | last post by:
I have a rather large class that i'm looking to serialize to Xml. Whenever it tries to serialize it I get this error message: An unhandled exception of type 'System.InvalidOperationException'...
3
by: ~~~ .NET Ed ~~~ | last post by:
Hello, I am having a problem trying to serialize this simple structure, I use it in a collection class that derives from CollectionBase but I noticed that at least for now the serialization...
1
by: chaitanyag | last post by:
Hi, I have my data stored in a set of classes (or structs, doesn't matter), which I am trying to serialize. These classes are stored in an ArrayList, which serializes ok when all the objects in...
1
by: Richard Atkinson | last post by:
I would appreciate help on the Microsoft Message Queue. Basically I would like to Serialize an entire class into a message queue and then reinstantiate the class in another application from the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.