I have created new class to read the data from xml file, which looks like :
public class Validations
{
public string id { get; set; }
public List<string> lhsList { get; set; }
public List<string> rhsList { get; set; }
}
XML I am trying to read is:
<root>
<Validation id="val3">
<lhs id='Estimated' />
<lhs id='Newqurter' />
<rhs id='Current' />
<rhs id='FirstQuarter' />
</Validation>
.
.
.
</root>
Code I have written to read the xml is :
List<Validations> vList = new List<Validations>();
vList = (from XElement xele in xdoc.Root.Elements()
select new Validations
{
id = xele.Attribute("id").Value.ToString(),
// lhsList = ((xele.Elements().FirstOrDefault(p => p.Name == "lhs").FirstAttribute.Value
// rhsList = ((xele.Elements().FirstOrDefault(p => p.Name == "rhs").FirstAttribute.Value
}
).ToList<Validations>();
How do read the List<lhsList> ?
I tried
lhsList = ((xele.Elements().FirstOrDefault(p => p.Name == "lhs").FirstAttribute.Value).ToList(),
But its not working as expected. What can be other ways to do this?
You can create the list of
lhselements as follows:This selects all the
lhselements that are children ofxele, then selects the value of their ‘id’ attribute. I’ll leave it to you to work out how to merge this with your code.