0

If I was retrieving a single element, I can use to pull an element named Name.

<li><span>System Name:</span>@(Model.Configuration.Root.Element("Name").Value)</li>

But, when I have multiple elements with the same name, it tells me Sequence contains no elements even though if I do it from code-behind, it gives me elements.

XML file:

<root>
    <Port Num="1">
        <Device>
            <Firmware>1.0</Firmware> 
        </Device>
    </Port>
    <Port Num="2">
        <Device>
            <Firmware>1.0</Firmware> 
        </Device>
    </Port>
</root>

CSHTML file:

<li><span>Port: 1</span>Firmware: @(
Model.Configuration.Root.Elements("Port")
.Where(a=>a.Attribute("Num").Equals("1")).First()
.Element("Device").Element("Firmware").Value)</li>

I want to retrieve the Firmware number (1.0) from Port #1.

3
  • Your question has nothing to do with Razor. Commented May 29, 2013 at 15:21
  • 1
    @Serge: That's not what he's comparing. Commented May 29, 2013 at 15:23
  • My bad, misread it ^^ Commented May 29, 2013 at 15:24

1 Answer 1

3

.Attribute() return an XAttribute object, which can never be equal to a string.

You want to compare the attribute's value:

Model.Configuration.Root.Elements("Port")
    .First(e => e.Attribute("Num").Value == "1")
    .Element("Device").Element("Firmware").Value
Sign up to request clarification or add additional context in comments.

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.