×

Discussion Board

Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    477

    Re: I'm getting an invalid exception in xdocument --> wp7

    Quote Originally Posted by Murugan india View Post
    hi....
    var details= from ack in xdoc.Descendants()
    If you know what 'type' ends up in details, that will explain why you're getting an invalid cast exception.

  2. #17
    Registered User
    Join Date
    Jul 2013
    Location
    chennai, TN
    Posts
    56

    Re: I'm getting an invalid exception in xdocument --> wp7

    hi.. vineet

    thank you..

    I'm still having an invalidcast exception even if i follow your coding... is there any other way

  3. #18
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    3,956

    Re: I'm getting an invalid exception in xdocument --> wp7

    Can you paste the xml you are trying to parse, along with the structure of class myclass.

  4. #19
    Registered User
    Join Date
    Jul 2013
    Location
    chennai, TN
    Posts
    56

    Re: I'm getting an invalid exception in xdocument --> wp7

    In DetailsModel.cs:

    in a method:

    public static myclass extractmethod(string result, ServiceModel sm)
    myclass fd=new myclass ();
    {
    XDocument xdoc = XDocument.Parse(result);
    var details= from ack in xdoc.Descendants()
    where ack.Name =="Details"
    select ack;


    myclass fd=(myclass )details; // invalidcast exception
    }

    In myclass.cs

    public class myclass
    {
    private string Id { get; set; }
    private string Source { get; set; }
    public string details{ get; set; }
    }

  5. #20
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    3,956

    Re: I'm getting an invalid exception in xdocument --> wp7

    Hmm not sure what you are actually trying to do, is it the value of descendant 'details' tag you are trying to fetch & save it?, try pasting the xml also.

  6. #21
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    477

    Re: I'm getting an invalid exception in xdocument --> wp7

    The answer is actually obvious, but it's best to try and help someone figure out the answer versus just giving to them, in which case they learn nothing.

    An 'invalid cast exception' happens when you try to convert from one type to another and no conversion is possible.

    Depending on how you use Linq in this case, it's either going to return an IEnumerable<MyClass> or IEnumerable<XElement>.

    Either way, casting to MyClass will throw an exception because they are NOT the same type. And if you don't understand why, then I suggest you read up on programming in C# and how to use the Debugger.

    http://msdn.microsoft.com/en-us/library/ms173105.aspx

  7. #22
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,570

    Re: I'm getting an invalid exception in xdocument --> wp7

    Note that in your new query you have not described how you get the invalid cast exception, that is why I assumed it is the same question as the previous one.

    Anyway I think I understand the current one. You are parsing an XML and want to populate an object. That is not done via casting. LINQ is powerful, but it can not cast a document fragment into a statically typed object/structure. You will have to create a method for your object which extracts the fields one by one from details. See http://www.codeproject.com/Tips/3669...ect-using-LINQ for some example.
    If you check http://www.codeproject.com/Tips/5909...-for-Beginners, its "The keyword var, object and collection initialization, and anonymous types" section shows how a "var" representing a complex structure will look like internally, it always has a type even if it is not seen in the code. And that ad-hoc type can not be casted to your myclass.

Similar Threads

  1. How to pass objects from one page to another in widows phone 8
    By sunder.tinwar in forum Windows Phone General
    Replies: 6
    Last Post: 2013-07-13, 13:31
  2. Replies: 11
    Last Post: 2008-03-08, 18:07
  3. how to get database values from xhtml page
    By pbalix in forum Browsing and Mark-ups
    Replies: 1
    Last Post: 2007-10-10, 07:31
  4. Replies: 0
    Last Post: 2005-02-17, 06:15
  5. Why I can't pass the variable in JSP to next page?
    By msc73jxs in forum Browsing and Mark-ups
    Replies: 0
    Last Post: 2002-08-31, 14:35

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •