我正在通过Linq查找此XML字符串中url
属性包含'.jpg'的每个标记'文件',只有一个'file'标记,但我检索了许多重复的元素
我的查询有什么问题?
string p = "<root><item itemid=\"1\" text=\"Club logo\" name=\"Item1\" mandatory=\"false\" selectionmode=\"single\" isGuidelinesEnabled=\"true\" description=\"VOBM - Enter abbreviated Club Name (e.g. Milan) and 2, 3 or 4 letter abbreviation (e.g. ACM)\"><objects><TextBox name=\"TextArea1\" " +
"mandatory=\"true\"><text><p>test </p></text></TextBox><Attachment name=\"Attachment1\" mandatory=\"false\" " +
"templatekey=\"XIS.Fbs.Stadium.SiteVisit.Template.Picture1\">" +
"<TemplateContent><p><span>%%Text</span></p> <table style=\"border: currentColor; border-collapse: collapse;\" border=\"1\" cellpadding=\"0\" " +
"cellspacing=\"0\"> <tbody><tr style=\"height: 27.8pt;\"> <td style=\"padding: 0cm; border: 1pt solid black; width: 226.25pt; height: 27.8pt;\" valign=\"top\" " +
"width=\"302\"> <p style=\"text-align: center;\" align=\"center\"><span><img style=\"width: 267px; height: 191px;\" alt=\"Picture_Missing.jpg\" " +
"src=\"/Repository/General/43/2/5/0/0/7/1/1/9/0/30.png\"></span> </p> <p style=\"text-align: center; page-break-after: avoid;\" align=\"center\"><span " +
"class=\"PictureCaption\"><span style=\"font-size: 9pt;\">%%PictureCaption</span></span></p> </td> </tr> </tbody></table> " +
"<p><span>&nbsp;</span></p></TemplateContent><file url=\"/Repository/Upload/SiteVisit/Report/204965/image (26).jpg\" name=\"image (26)\" /></Attachment></objects></item></root>";
var doc = XDocument.Parse(p);
var pp = p.Select(x => doc.Descendants("file").Where(y => y.Attribute("url").Value.Contains(".jpg"))).Distinct().ToList();
foreach (var i in pp)
{
Console.WriteLine(i);
}
答案 0 :(得分:2)
看起来p.Select
在这里是多余的:
var pp = p.Select(x => doc.Descendants("file").Where(y => y.Attribute("url").Value.Contains(".jpg")))
.Distinct()
.ToList();
换句话说,它应该很简单
var pp = doc.Descendants("file")
.Where(y => y.Attribute("url").Value.Contains(".jpg"))
.Distinct()
.ToList();
答案 1 :(得分:2)
你为什么要搜索p。通过文档列举。
var pp = doc.Descendants("file").Where(y => y.Attribute("url").Value.Contains(".jpg")).Distinct().ToList();