Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Linq To Xml

Linq To Xml

Scheduled Pinned Locked Moved LINQ
helpcsharplinqxmltutorial
4 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koleraba
    wrote on last edited by
    #1

    Hi I have a problem regarding Linq to XML. I will try to explain my problem on a simple example. My Xml looks like: <variables>    <variable>          <name>A</name>          <type>int</type>          <value>11</value>    <variable> <variable> In my code I have a method which returs all variables // Variable class public class Variable {    string name{get;set;}    Type DataType{get;set;}    object Value{get;set;} } //Method which reads XML file public List<Variable> GetVariables() {    var records = from record in document.Descendants("Variable")                            select new Variable                            {                               Name = (string)record.Element("name"),                               DataType = Support.GetType((string)record.Element("type"),                               DefaultValue = Support.Convert((string)record.Element("value"), Support.GetType((string)record.Element("type")) //????????                            };    //Convert to List and return } In the method GetVariabes() I call two methods, which are defined in the static class Support. The first one constructs a Type object from type name and the second one converts the string to type which is specified by second parameter. In the example above, I get the second parameter from another call to method Support.GetType() even though the parameter was set in the previous line(Property DataType in the variable class). Is it possible to get the DataType from previous line - so the problematic line woule look something like: DefaultValue = Support.Convert((string)record.Element("value"), DataType) Any advice will be appreciated, Thank you for your help Uros

    E P 2 Replies Last reply
    0
    • K koleraba

      Hi I have a problem regarding Linq to XML. I will try to explain my problem on a simple example. My Xml looks like: <variables>    <variable>          <name>A</name>          <type>int</type>          <value>11</value>    <variable> <variable> In my code I have a method which returs all variables // Variable class public class Variable {    string name{get;set;}    Type DataType{get;set;}    object Value{get;set;} } //Method which reads XML file public List<Variable> GetVariables() {    var records = from record in document.Descendants("Variable")                            select new Variable                            {                               Name = (string)record.Element("name"),                               DataType = Support.GetType((string)record.Element("type"),                               DefaultValue = Support.Convert((string)record.Element("value"), Support.GetType((string)record.Element("type")) //????????                            };    //Convert to List and return } In the method GetVariabes() I call two methods, which are defined in the static class Support. The first one constructs a Type object from type name and the second one converts the string to type which is specified by second parameter. In the example above, I get the second parameter from another call to method Support.GetType() even though the parameter was set in the previous line(Property DataType in the variable class). Is it possible to get the DataType from previous line - so the problematic line woule look something like: DefaultValue = Support.Convert((string)record.Element("value"), DataType) Any advice will be appreciated, Thank you for your help Uros

      E Offline
      E Offline
      Eslam Afifi
      wrote on last edited by
      #2

      Using the let clause[^] your query becomes

      var records = from record in document.Descendants("Variable")
      let type = Support.GetType((string)record.Element("type"))
      select new Variable
      {
      Name = (string)record.Element("name"),
      DataType = type,
      DefaultValue = Support.Convert((string)record.Element("value"), type)
      };

      Eslam Afifi

      K 1 Reply Last reply
      0
      • E Eslam Afifi

        Using the let clause[^] your query becomes

        var records = from record in document.Descendants("Variable")
        let type = Support.GetType((string)record.Element("type"))
        select new Variable
        {
        Name = (string)record.Element("name"),
        DataType = type,
        DefaultValue = Support.Convert((string)record.Element("value"), type)
        };

        Eslam Afifi

        K Offline
        K Offline
        koleraba
        wrote on last edited by
        #3

        That solved my problem. Thank you Eslam. Uroš

        1 Reply Last reply
        0
        • K koleraba

          Hi I have a problem regarding Linq to XML. I will try to explain my problem on a simple example. My Xml looks like: <variables>    <variable>          <name>A</name>          <type>int</type>          <value>11</value>    <variable> <variable> In my code I have a method which returs all variables // Variable class public class Variable {    string name{get;set;}    Type DataType{get;set;}    object Value{get;set;} } //Method which reads XML file public List<Variable> GetVariables() {    var records = from record in document.Descendants("Variable")                            select new Variable                            {                               Name = (string)record.Element("name"),                               DataType = Support.GetType((string)record.Element("type"),                               DefaultValue = Support.Convert((string)record.Element("value"), Support.GetType((string)record.Element("type")) //????????                            };    //Convert to List and return } In the method GetVariabes() I call two methods, which are defined in the static class Support. The first one constructs a Type object from type name and the second one converts the string to type which is specified by second parameter. In the example above, I get the second parameter from another call to method Support.GetType() even though the parameter was set in the previous line(Property DataType in the variable class). Is it possible to get the DataType from previous line - so the problematic line woule look something like: DefaultValue = Support.Convert((string)record.Element("value"), DataType) Any advice will be appreciated, Thank you for your help Uros

          P Offline
          P Offline
          Palash Biswas
          wrote on last edited by
          #4

          you can not use DefaultValue = Support.Convert((string)record.Element("value"), DataType) the previous line DataType Calculation was not calculated until you use the Datatype Property. and also not calculated DefaultValue until you access it.So it is not executed properly..

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups