473,396 Members | 1,938 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,396 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 10414
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.