473,581 Members | 6,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem serializing type System.Xml.XmlE lement

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 "ResultNode s" as
System.Xml.XmlE lements but when I try and serialize (serialise) my
result object I get the following exception:

"System.Invalid OperationExcept ion" - "There was an error generating the
XML document"
"The type System.Xml.XmlE lement may not be used in this context."
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o, XmlSerializerNa mespaces namespaces, String
encodingStyle, String id)
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o, XmlSerializerNa mespaces namespaces, String
encodingStyle)
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o, XmlSerializerNa mespaces namespaces)
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o)
at <My Source Code>

The XSD for my ResultNodes collection is:
<xs:complexTy pe name="ResultNod es">
<xs:sequence>
<xs:element name="ResultNod e" type="xs:anyTyp e" minOccurs="0"
maxOccurs="unbo unded" />
</xs:sequence>
</xs:complexType>

Can System.Xml.XmlE lements 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>Leggette r.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 10425
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.goo glegroups.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 "ResultNode s" as
System.Xml.XmlE lements but when I try and serialize (serialise) my
result object I get the following exception:

"System.Invalid OperationExcept ion" - "There was an error generating the
XML document"
"The type System.Xml.XmlE lement may not be used in this context."
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o, XmlSerializerNa mespaces namespaces, String
encodingStyle, String id)
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o, XmlSerializerNa mespaces namespaces, String
encodingStyle)
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o, XmlSerializerNa mespaces namespaces)
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o)
at <My Source Code>

The XSD for my ResultNodes collection is:
<xs:complexTy pe name="ResultNod es">
<xs:sequence>
<xs:element name="ResultNod e" type="xs:anyTyp e" minOccurs="0"
maxOccurs="unbo unded" />
</xs:sequence>
</xs:complexType>

Can System.Xml.XmlE lements 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>Leggette r.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...@y ahoo.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...@legg etter.co.ukwrot e in messagenews:11* *************** ******@a75g2000 cwd.googlegroup s.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 "ResultNode s" as
System.Xml.XmlE lements but when I try and serialize (serialise) my
result object I get the following exception:
"System.Invalid OperationExcept ion" - "There was an error generating the
XML document"
"The type System.Xml.XmlE lement may not be used in this context."
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o, XmlSerializerNa mespaces namespaces, String
encodingStyle, String id)
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o, XmlSerializerNa mespaces namespaces, String
encodingStyle)
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o, XmlSerializerNa mespaces namespaces)
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o)
at <My Source Code>
The XSD for my ResultNodes collection is:
<xs:complexTy pe name="ResultNod es">
<xs:sequence>
<xs:element name="ResultNod e" type="xs:anyTyp e" minOccurs="0"
maxOccurs="unbo unded" />
</xs:sequence>
</xs:complexType>
Can System.Xml.XmlE lements 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>Leggette r.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...@y ahoo.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...@legg etter.co.ukwrot e in messagenews:11* *************** ******@a75g2000 cwd.googlegroup s.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 "ResultNode s" as
System.Xml.XmlE lements but when I try and serialize (serialise) my
result object I get the following exception:
"System.Invalid OperationExcept ion" - "There was an error generating the
XML document"
"The type System.Xml.XmlE lement may not be used in this context."
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o, XmlSerializerNa mespaces namespaces, String
encodingStyle, String id)
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o, XmlSerializerNa mespaces namespaces, String
encodingStyle)
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o, XmlSerializerNa mespaces namespaces)
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o)
at <My Source Code>
The XSD for my ResultNodes collection is:
<xs:complexTy pe name="ResultNod es">
<xs:sequence>
<xs:element name="ResultNod e" type="xs:anyTyp e" minOccurs="0"
maxOccurs="unbo unded" />
</xs:sequence>
</xs:complexType>
Can System.Xml.XmlE lements 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>Leggette r.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:complexTy pe name="ResultNod es">
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbo unded" />
</xs:sequence>
</xs:complexType>

2) Once the C# class had been generated using XSDObjectGen.ex e I would
then have to update the attribute applied to the generated
ResultNodeColle ction:

//
[XmlElement(Type =typeof(object) ,ElementName="R esultNode",IsNu llable=false,Fo rm=XmlSchemaFor m.Qualified,Nam espace=Declarat ions.SchemaVers ion)]
[XmlAnyElement(N amespace=Declar ations.SchemaVe rsion)]
[EditorBrowsable (EditorBrowsabl eState.Advanced )]
public ResultNodeColle ction __ResultNodeCol lection;

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.ex e 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
382
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
2128
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 entry to the task collection property (of Document). public TaskCollection Tasks {
2
3285
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 ‘DataItem’ public class DataItem { public DataItem() {}
4
2724
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 am using to create a sample XML file. I am trying to dimension the ArrayOfJudgeEntity to have two sets of the JudgeTableEntity values. When I run...
6
3531
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"> <Directories basePath="//OpicsPlus/Source/Release" /> </Sync>
1
1578
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' occurred in system.xml.dll Additional information: There was an error reflecting 'VestaQuoteV1.Case'.
3
10802
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 exception ocurrs when i try to serialize this structure (see below). The idea is that it would be serialized to: < Param type="..." method="Get"...
1
4508
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 the list are of the same type. For example, if i have public class A { string A1;
1
3651
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 message queue with all properties, variables etc in tact. What is the best way to do this ie which formatter to use and how do you deal with...
0
7783
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8134
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8296
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7880
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8157
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6539
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5350
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1394
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.