Reputation: 23
I have a xml file with nodes with the same name
<Test>
<testing>
<testId>2233</testId>
<name>frank</name>
<machine>3</machine>
<img>1.jpg</img>
<img>5.jpg</img>
<img>001.jpg</img>
<img>100.jpg</img>
<img>1125.jpg</img>
</testing>
</Test>
The controller
public ActionResult TestMachine(int id = 0)
{
XDocument Xdoc = XDocument.Load("test.xml");
var Test = Xdoc.Descendants("testing").Select
(imm => new test
{
TestId = Convert.ToInt32(imm.Element("testId").Value),
Name = imm.Element("name").Value,
Machine = imm.Element("machine").Value,
Image = imm.Elements("img").Select(img => new Images {
Url = img.Element("img").ElementValueNull()
}).ToList()
}).Where(i=> i.TestId == id).FirstOrDefault();
return View(test);
}
The value of list img are empty any suggestions appreciated
Upvotes: 1
Views: 71
Reputation: 6374
Please try the following:
public ActionResult TestMachine(int id = 0)
{
XDocument Xdoc = XDocument.Load("test.xml");
var Test = Xdoc.Descendants("testing").Select
(imm => new test
{
TestId = Convert.ToInt32(imm.Element("testId").Value),
Name = imm.Element("name").Value,
Machine = imm.Element("machine").Value,
Image = imm.Elements("img").Select(img => new Images {
Url = img.Value
}).ToList()
}).Where(i=> i.TestId == id).FirstOrDefault();
return View(test);
}
Upvotes: 0
Reputation: 1716
I think that the problem is with
...
Image = imm.Elements("img").Select(img => new Images {
Url = img.Element("img").ElementValueNull()
}).ToList()
because img is already an <img>
element, but you query it for its child <img>
element.
Change it to
Image = imm.Elements("img").Select(img => new Images {
Url = img.ElementValueNull()
}).ToList()
Upvotes: 1