C # Linq over XML => Lambda expression

I have an XML document that consists of several of the following elements:

- <LabelFieldBO>
  <Height>23</Height> 
  <Width>100</Width> 
  <Top>32</Top> 
  <Left>128</Left> 
  <FieldName>field4</FieldName> 
  <Text>aoi_name</Text> 
  <DataColumn>aoi_name</DataColumn> 
  <FontFamily>Arial</FontFamily> 
  <FontStyle>Regular</FontStyle> 
  <FontSize>8.25</FontSize> 
  <Rotation>0</Rotation> 
  <LabelName /> 
  <LabelHeight>0</LabelHeight> 
  <LabelWidth>0</LabelWidth> 
  <BarCoded>false</BarCoded> 
  </LabelFieldBO>

I figured out how to find the element where LabelName = 'container'. But I'm not very good at lambda expressions and would like to know how to access the information in my LINQ results. Lambda expressions may also not be able to go. I am open to any suggestions.

var dimensions = from field in xml.Elements("LabelFieldBO")
                             where field.Element("LabelName").Value == "container"
                             select field;

Thank.

EDIT: I'm trying to figure out how to get LabelHeight and LabelWidth from XML, where LabelName = "container"

+3
source share
2 answers
from field in xml.Elements("LabelFieldBO")  
where field.Element("LabelName").Value == "container"  
select new   
{  
    LabelHeight = field.Element("LabelHeight").Value,  
    LabelWidth = field.Element("LabelWidth").Value  
}

IEnumerable (LabelWeight LabelWidth). IEnumerable LabelFieldB0 LabelName = "container".

, "" , - :

var containerLabels =   
    from field in xml.Elements("LabelFieldBO")  
    where field.Element("LabelName").Value == "container"  
    select new   
    {  
        LabelHeight = field.Element("LabelHeight").Value,  
        LabelWidth = field.Element("LabelWidth").Value  
    } 

foreach (var containerLabel in containerLabels)  
{  
    Console.WriteLine(containerLabel.LabelHeight + " "
        + containerLabel.LabelWidth);  
}
+1

, , .

var result = doc.Elements("LabelFieldBo")
                 .Where(x => x.Element("LabelName").Value == "container")
                 .Select(x =>
                     new { 
                         Name = x.Element("LabelName").Value,
                         Height = x.Element("LabelHeight").Value,
                         Width = x.Element("LabelWidth").Value
                 }
             ); 
+5

Source: https://habr.com/ru/post/1735651/


All Articles