469,360 Members | 1,807 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 10196
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Daniel Lidström | last post: by
2 posts views Thread by Werner B. Strydom | last post: by
2 posts views Thread by Aleksei Guzev | last post: by
4 posts views Thread by Wayne Wengert | last post: by
6 posts views Thread by Ferrari, Eduardo | last post: by
1 post views Thread by Jax | last post: by
3 posts views Thread by ~~~ .NET Ed ~~~ | last post: by
1 post views Thread by chaitanyag | last post: by
1 post views Thread by Richard Atkinson | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.