Linq To Xml
-
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
-
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
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
-
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
-
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
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..