Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get values from specific fields from a string which is XML format.

My Code:
C#
static void Main(string[] args)
        {
            String str;
            str = "<layer> <auto_delta>null</auto_delta> <frame-number>5236</frame-number> <nb_frames>45001</nb_frames> <frames-left>39765</frames-left> <frame-age>160</frame-age> <producer>  <type>ffmpeg</type>  <filename>media\\JTV.AVI</filename>  <width>720</width>  <height>576</height>  <progressive>true</progressive>  <fps>25</fps> <loop>false</loop> <frame-number>5237</frame-number> <nb-frames>45001</nb-frames>  <file-frame-number>0</file-frame-number>  <file-nb-frames>45001</file-nb-frames> </producer> <background> <producer> <type>empty-producer</type> </producer> </background> <index>0</index></layer>";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(str);
            string frame_path = "layer/producer";
            var nodes_frame = xmlDoc.SelectNodes(frame_path);

            try
            {
                foreach (XmlNode childrenNode_frame in nodes_frame)
                {
                    Console.WriteLine("Frames: " + childrenNode_frame.SelectSingleNode("//file-nb-frames").Value.ToString() + " Frame Rate: " + childrenNode_frame.SelectSingleNode("//fps").Value.ToString());
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
            }

            Console.Read();
        }


Now getting Exception Object re reference not set in
C#
Console.WriteLine("Frames: " + childrenNode_frame.SelectSingleNode("//file-nb-frames").Value.ToString() + " Frame Rate: " + childrenNode_frame.SelectSingleNode("//fps").Value.ToString());
this line.

Please help me. Thanks in advance.

What I have tried:

I have used Load() method. Than I have got another exception as like this: "Additional information: Illegal characters in path."
Posted
Updated 16-Jun-16 23:59pm

1 solution

Replace:
C#
.Value.ToString()

With:
C#
.InnerText
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900