473,410 Members | 1,907 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,410 software developers and data experts.

Problem with inheriting from XmlElement class

Hi!
I have this class:
public class StoredItemElement : XmlElement

{

protected internal StoredItemElement(string prefix, string localName, string
namespaceUri, XmlDocument doc) : base(prefix, localName, namespaceUri, doc)
{ }

....

}

which inherits from XmlElement.

But when I try this:

XmlDocument doc = new XmlDocument();

doc.Load("File.xml");

StoredItemElement element =
(StoredItemElement)doc.SelectSingleNode("//item");

I get InvalidCastException, but I don't know where the problem is.

Can you help me?

Thanks in advance.
Nov 12 '05 #1
2 3293


Compic wrote:

I have this class:
public class StoredItemElement : XmlElement

{

protected internal StoredItemElement(string prefix, string localName, string
namespaceUri, XmlDocument doc) : base(prefix, localName, namespaceUri, doc)
{ }

...

}

which inherits from XmlElement.

But when I try this:

XmlDocument doc = new XmlDocument();

doc.Load("File.xml");

StoredItemElement element =
(StoredItemElement)doc.SelectSingleNode("//item");

I get InvalidCastException, but I don't know where the problem is.


You can't cast an instance of the class XmlElement to an instance of
your derived class StoredItemElement, try
doc.SelectSingleNode("//item").getType()
and you will find that it shows
System.Xml.XmlElement
and you can't simply cast that to your StoredItemElement.
You would need to overwrite the SelectSingleNode method to return a
StoredItemElement instance when possible if you want to be able to cast
its result to that type.

That is simply how casting works in C#/.NET I think, nothing you could
change about that.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
Compic wrote:
XmlDocument doc = new XmlDocument();

doc.Load("File.xml");


That's wrong. You should also extend XmlDocument and override
CreateElement method. See
http://msdn.microsoft.com/library/de...tendingdom.asp

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #3

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

Similar topics

1
by: john diss | last post by:
hello there everyone.. I have created a class called "ProcessLog" inheriting from XmlDocument and two classes ("UploadedItem", "ProcessError") inheriting from XmlElement. I then have two...
2
by: Jiho Han | last post by:
I generated a class from a schema. One of the fields are typed as System.Guid. Perfect. The only problem is when this class serializes, the guid field serializes as...
6
by: Daniel Lidström | last post by:
Hi, how do I synchronize (to XML using XmlSerializer) something to this: <Alignments name="Road Project"> <Alignment name="Centerline"> <CoordGeom> <Line staStart="0"> <Start>2000...
0
by: Vicenç Masanas | last post by:
I want to specialize XmlElementAttribute so I can control how an specific property is serialized, but I cannot get what methods should I override to accomplish this. <Serializable()> Public...
5
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
0
by: Michael Jackson | last post by:
I have attempted to mark up a service and it's methods so that it doesn't require the SOAPAction HTTP header to resolve the methods being called, this is done from first element in <SOAP-ENV:Body>...
0
by: Julien Sobrier | last post by:
Hello, I have an XML that looks like this (simplified): <schemafile> <schemalist module="xxxx"> <schema> ... </schema> <schema> .... <schema>
8
by: Gonza | last post by:
Hi group, i'm getting the following excepction while trying to serialize a class: Types 'NYC.TrueFinance.BusinessEntities.ExtendedBaseEntityTypeData.EntityTypes' and...
4
by: Varangian | last post by:
Hello something I want to ask...... why on earth you cannot inherit from XmlElement???? .... or you can but XmlElement constructor is protected or private and so you cannot define an empty...
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?
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
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.