I am trying to write a Linq to XML query that returns the keyword back and includes the attribute (mediatype)
Consider the following XML data ...
<?xml version="1.0" encoding="utf-8"?>
<media>
<photos>
<photo mediatype="photo" photographer="Jag_cz" description="Colored splashes in abstract shape, isolated on white background" id="16" name="50623755_F.jpg" folder="HR Headset">
<keywords>
<keyword>fish</keyword>
<keyword>abstract</keyword>
</keywords>
</photo>
</photos>
<videos>
<video mediatype="video" description="Bear by a stream" folder="streamfolder" name="stream.mp4">
<keywords>
<keyword>stream</keyword>
<keyword>river</keyword>
<keyword>water</keyword>
</keywords>
</video>
<video mediatype="video" description="Stream with a bear" folder="bearfolder" name="bear.mp4">
<keywords>
<keyword>salmon</keyword>
<keyword>fish</keyword>
</keywords>
</video>
</videos>
</media>
There are photocells and video elements. Each element has a mediattype attribute.
I want to return a query for each keyword with its media type.
Something like that..
mediatype keyword
--------- -------
photo fish
photo abstract
video stream
video river
video water
video salmon
video fish
I managed to cancel the keywords using the following code ...
using System;
using System.Xml.Linq;
using System.Linq;
class Program
{
public static void Main(string[] args)
{
String strPath = @"C:\videodata\media.xml";
XElement xEle = XElement.Load(strPath);
var keywordquery = from k in xEle.Descendants("keyword")
select new
{
keyword = (string)k.Value
};
foreach (var k in keywordquery)
{
Console.WriteLine(k.keyword);
}
Console.WriteLine("Press <enter> to continue");
Console.ReadLine();
}
}
However, I am stuck in returning the mediatype attribute. This attribute exists at a different level than the keyword.
source
share