3

I have an xml document which consists of a number of the following:

- <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 have figured out how to find the element where LabelName = 'container'. But I am not well versed with lambda expressions and would like to know how to access the information within my LINQ results. Lambda expressions may not be the way to go either. I am open to any suggestions.

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

Thanks.

EDIT: What I am trying to figure out is how to get the LabelHeight and LabelWidth out of the XML where LabelName = "container"

1
  • It is not exactly clear what you want to do, could you please describe it a bit more exact. Possible with samples of the results that you are expecting. Commented Mar 5, 2010 at 20:45

2 Answers 2

5

The following code create a new anonymous object that contains the label name, width, and height.

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
                 }
             ); 
Sign up to request clarification or add additional context in comments.

Comments

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

This returns an IEnumerable of anonymous types with two properties (LabelWeight and LabelWidth). Each object in the IEnumerable represents a LabelFieldB0 with LabelName = "container".

So, you can "get at" your data doing something like:

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);  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.