Get xml attribute name and value along with descendants

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have this xml and I want to get each section attribute. I will show what I mean.
Here is my code that I have so far to get the XML.public List<string[]> GetAllInfo(string file)
{
List<string[]> list = new List<string[]>();
if (this.Exceute(file))
{
XDocument xDoc = XDocument.Load(Path.Combine(Environment.CurrentDirectory, "MediaInfo", "test.xml"));
foreach (XElement exel in xDoc.Root.Element("File").Elements("track").DescendantNodesAndSelf())
{
if (exel.NodeType == XmlNodeType.Attribute)
{
if (list != null)
{
list.Add(new string[] { "", "" });
}

list.Add(new string[] { exel.FirstAttribute.Name.ToString(), exel.FirstAttribute.Value });
}

list.Add(new string[] { exel.Name.ToString(), exel.Value });
}
}

return list;
}

XML looks like this.<?xml version="1.0" encoding="UTF-8"?>
<Mediainfo version="0.7.62
<File>
<track type="General
<Complete_name>C:/Users/CmptrPrgmr/Desktop/VID_20130204_234541.mp4</Complete_name>
<Format>MPEG-4</Format>
<Format_profile>Base Media</Format_profile>
<Codec_ID>isom</Codec_ID>
<File_size>714 MiB</File_size>
<Duration>10mn 18s</Duration>
<Overall_bit_rate>9 692 Kbps</Overall_bit_rate>
</track>

<track type="Video
<ID>1</ID>
<Format>AVC</Format>
<Format_Info>Advanced Video Codec</Format_Info>
<Format_profile>Baseline@L4.1</Format_profile>
<Format_settings__CABAC>No</Format_settings__CABAC>
<Format_settings__ReFrames>1 frame</Format_settings__ReFrames>
<Format_settings__GOP>M=1, N=30</Format_settings__GOP>
<Codec_ID>avc1</Codec_ID>
<Codec_ID_Info>Advanced Video Coding</Codec_ID_Info>
<Duration>10mn 18s</Duration>
<Source_duration>10mn 18s</Source_duration>
<Bit_rate>9 594 Kbps</Bit_rate>
<Width>1 920 pixels</Width>
<Height>1 080 pixels</Height>
<Display_aspect_ratio>16:9</Display_aspect_ratio>
<Frame_rate_mode>Variable</Frame_rate_mode>
<Frame_rate>24.000 fps</Frame_rate>
<Minimum_frame_rate>6.000 fps</Minimum_frame_rate>
<Maximum_frame_rate>24.168 fps</Maximum_frame_rate>
<Color_space>YUV</Color_space>
<Chroma_subsampling>4:2:0</Chroma_subsampling>
<Bit_depth>8 bits</Bit_depth>
<Scan_type>Progressive</Scan_type>
<Bits__Pixel_Frame_>0.193</Bits__Pixel_Frame_>
<Stream_size>707 MiB (99%)</Stream_size>
<Source_stream_size>707 MiB (99%)</Source_stream_size>
<Title>VideoHandle</Title>
<Language>English</Language>
<mdhd_Duration>618048</mdhd_Duration>
</track>

<track type="Audio
<ID>2</ID>
<Format>AAC</Format>
<Format_Info>Advanced Audio Codec</Format_Info>
<Format_profile>LC</Format_profile>
<Codec_ID>40</Codec_ID>
<Duration>10mn 18s</Duration>
<Bit_rate_mode>Constant</Bit_rate_mode>
<Bit_rate>96.0 Kbps</Bit_rate>
<Channel_s_>1 channel</Channel_s_>
<Channel_positions>Front: C</Channel_positions>
<Sampling_rate>48.0 KHz</Sampling_rate>
<Compression_mode>Lossy</Compression_mode>
<Stream_size>7.08 MiB (1%)</Stream_size>
<Title>SoundHandle</Title>
<Language>English</Language>
</track>

</File>
</Mediainfo>


I want to make it look like this.
track: General
Complete_Name: blablabla
More blablabla
track: Video
ID: 1
More bla
track: Audio
ID: 2
More blabla
I get get all of it the way I want, but I cant the name of the track.
Could someone also please show me how I could just go search straight to a track because sometimes there are nodes that have the same name, for example, ID. So I was thinking that a way to do that part would be to to foreach attribute, if that attribute has the correct name, then I do foreach element in that attribute, if that makes sense at all. :p
Thanks guys. I know I have posted a lot of questions in the past few days. Some havent been answered, but thats okay. Youre all trying your best.

View the full article
 
Back
Top