Question XDocument get value by specific element & key/tag

Anime4000

New member
Joined
Jan 20, 2016
Messages
1
Programming Experience
5-10
Hello...
I have an XML file which is generated by FFprobe (FFmpeg), Code:

C#:
<?xml version="1.0" encoding="UTF-8"?>
<ffprobe>
    <streams>
        <stream index="0" codec_name="h264" codec_long_name="H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10" profile="High" codec_type="video" codec_time_base="1/120" codec_tag_string="avc1" codec_tag="0x31637661" width="1920" height="1080" coded_width="1920" coded_height="1088" has_b_frames="2" sample_aspect_ratio="1:1" display_aspect_ratio="16:9" pix_fmt="yuv420p" level="41" color_range="tv" color_space="bt709" color_transfer="bt709" color_primaries="bt709" chroma_location="left" refs="4" is_avc="true" nal_length_size="4" r_frame_rate="60/1" avg_frame_rate="60/1" time_base="1/90000" start_pts="0" start_time="0.000000" duration_ts="28867500" duration="320.750000" bit_rate="8901225" bits_per_raw_sample="8" nb_frames="19245">
            <disposition default="1" dub="0" original="0" comment="0" lyrics="0" karaoke="0" forced="0" hearing_impaired="0" visual_impaired="0" clean_effects="0" attached_pic="0"/>
            <tag key="creation_time" value="2015-02-10 19:11:09"/>
            <tag key="language" value="und"/>
            <tag key="encoder" value="JVT/AVC Coding"/>
        </stream>
        <stream index="1" codec_name="aac" codec_long_name="AAC (Advanced Audio Coding)" profile="LC" codec_type="audio" codec_time_base="1/48000" codec_tag_string="mp4a" codec_tag="0x6134706d" sample_fmt="fltp" sample_rate="48000" channels="2" channel_layout="stereo" bits_per_sample="0" r_frame_rate="0/0" avg_frame_rate="0/0" time_base="1/48000" start_pts="0" start_time="0.000000" duration_ts="15396864" duration="320.768000" bit_rate="127959" max_bit_rate="150024" nb_frames="15036">
            <disposition default="1" dub="0" original="0" comment="0" lyrics="0" karaoke="0" forced="0" hearing_impaired="0" visual_impaired="0" clean_effects="0" attached_pic="0"/>
            <tag key="creation_time" value="2015-02-10 19:11:09"/>
            <tag key="language" value="und"/>
        </stream>
    </streams>

    <format filename="D:\Users\Anime4000\Videos\Game\001.mp4" nb_streams="2" nb_programs="0" format_name="mov,mp4,m4a,3gp,3g2,mj2" format_long_name="QuickTime / MOV" start_time="0.000000" duration="320.768000" size="362326534" bit_rate="9036475" probe_score="100">
        <tag key="major_brand" value="mp42"/>
        <tag key="minor_version" value="0"/>
        <tag key="compatible_brands" value="mp42isomavc1"/>
        <tag key="creation_time" value="2015-02-10 19:11:09"/>
        <tag key="encoder" value="HandBrake 0.9.9 2013052900"/>
    </format>
</ffprobe>

C# 6.0 Code:
C#:
            var xml = XDocument.Load(file);
            var video = from a in xml.Descendants("stream")
                        where string.Equals("video", (string)a.Attribute("codec_type"))
                        select new
                        {
                            id = (int)a.Attribute("index"),
                            lang = a.Element("tag").Attribute("value").Value, // <---- this one
                            codec = a.Attribute("codec_name").Value,
                            pixfmt = a.Attribute("pix_fmt").Value,
                            bpc = a.Attribute("bits_per_raw_sample")?.Value,
                            width = a.Attribute("width").Value,
                            height = a.Attribute("height").Value,
                            fps = a.Attribute("r_frame_rate").Value,
                            framecount = a.Attribute("nb_frames")?.Value,
                        };

Everything was fine until inconsistent arrangement, how I can specifically get value by this tag & key? <tag key="language"

Full source code on GitHub
 

Latest posts

Top Bottom