473,795 Members | 2,968 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with inheriting from XmlElement class

Hi!
I have this class:
public class StoredItemEleme nt : XmlElement

{

protected internal StoredItemEleme nt(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");

StoredItemEleme nt element =
(StoredItemElem ent)doc.SelectS ingleNode("//item");

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

Can you help me?

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


Compic wrote:

I have this class:
public class StoredItemEleme nt : XmlElement

{

protected internal StoredItemEleme nt(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");

StoredItemEleme nt element =
(StoredItemElem ent)doc.SelectS ingleNode("//item");

I get InvalidCastExce ption, 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 StoredItemEleme nt, try
doc.SelectSingl eNode("//item").getType( )
and you will find that it shows
System.Xml.XmlE lement
and you can't simply cast that to your StoredItemEleme nt.
You would need to overwrite the SelectSingleNod e method to return a
StoredItemEleme nt 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
2447
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 classes ("UploadedTemplate", "UploadedPresentation") which inherit from "UploadedItem")... so far so good. I have overriden the CreateElement method of "ProccessLog" and it creates the correct elements as expected. The problem is I need to override...
2
13406
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 8c4a969b-2aa4-4679-b170-d9f6441f7c6d, when I need it as {8A3ACA06-A7DE-41A5-B584-063E7CF391BB}, all upper, and with braces. Is there a way to force the XmlSerializer to serialize as the latter format? I tried to turn the field into a string property with private field...
6
2138
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 6000</Start> <End>2186.841 6068.005</End> </Line>
0
1224
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 Class class1 <XmlElement("a")> Public a As String <MyXmlElement("b")> Public b As Date <XmlElement("c")> Public c As Integer End Class
5
2144
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 run the class it gives an error that "System.Xml.XmlNode.XmlNode() is inaccessible due to its protection level". This error comes because XmlNode has not any public constructor. I found XmlNode has two constructor but both are private or friend...
0
2296
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> and the request and responses are not suffixed with Request and Response. Is there some more I could do with the attributes on the class that defines the WS and on the methods. the code for the WS etc is below. This I can get to work POST...
0
974
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
1643
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 'NYC.TrueFinance.BusinessEntities.BaseEntityTypeData.EntityTypes' both use the XML type name, 'EntityTypes', from namespace ''. Use XML attributes to specify a unique XML name and/or namespace for the type. The code for the class in question (and it's superclass) is the
4
4373
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 constructor. Even when I tried building a constructor with the exact parameters like XmlElement it wouldn't work!!!! "Error 1 No overload for method 'XmlElement' takes '0' arguments"
0
9519
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6783
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.