Click here to Skip to main content
15,942,094 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I am trying to read a stream which is in xml format to retrieve specific field values.

My Code:
C#
using (var client = new TcpClient(host, port))
            {
                var reader = new StreamReader(client.GetStream());
                var writer = new StreamWriter(client.GetStream());
                writer.WriteLine(cmd);
                writer.Flush();
                var reply = reader.ReadLine(); 
                if (reply.Contains("202"))
                    {
                        while (reply.Length > 0)
                        {
                            reply = reader.ReadLine();
                            XmlDocument xmlDoc = new XmlDocument();
                            
                            String str = reply;
                            str = str.Replace("202 PLAY OK", "");
                            str = str.Replace("201 INFO OK", "");
                            xmlDoc.LoadXml(str);
                            string frame_path = "layer/producer";
                            //string fps_path = "layer/producer/fps";
                            var nodes_frame = xmlDoc.SelectNodes(frame_path);
                            //var nodes_fps = xmlDoc.SelectNodes(fps_path);

                            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());
                            }
                           
                        }
                        
                    }
                    

                }
                
                Console.Read();
            }


Now I am getting the exception "root element is missing";

In my console the xml string is as like this:
XML
<?xml version="1.0" encoding="utf-8"?>
<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>


What I have tried:

I have tried by using this command:
var reply = reader.ReadToEnd();

Than there is only black window in my console.
Posted
Updated 16-Jun-16 23:39pm
v2
Comments
Richard Deeming 16-Jun-16 12:50pm    
Are you sure that the entire XML document is on a single line?

Debug your code and examine the str variable just before the xmlDoc.LoadXml(str); call; does it contain a complete and valid XML document?
Member 10261487 16-Jun-16 12:54pm    
yes. it contain a complete and valid XML document.
It shows:

<layer>
<auto_delta>null

<frame-number>5236</frame-number>
<nb_frames>45001
<frames-left>39765</frames-left>
<frame-age>160</frame-age>
<producer>
<type>ffmpeg
<filename>media\JTV.AVI
<width>720
<height>576
<progressive>true</progressive>
<fps>25
<loop>false
<frame-number>5237</frame-number>
<nb-frames>45001
<file-frame-number>0
<file-nb-frames>45001
</producer>
<background>
<producer>
<type>empty-producer
</producer>

<index>0


Is it not valid ?
Richard Deeming 16-Jun-16 12:58pm    
1) That's not a single line;
2) That's not a complete and valid XML document;
Sergey Alexandrovich Kryukov 16-Jun-16 13:16pm    
Sure, but the XML file itself is valid. :-)
I tried to explain what to do in Solution 1. Your comments are credited, of course.
—SA

Richard Deeming is right — please see his comments to the question.

Yes, your XML file is valid. If you parsed it, it would perfectly parse.

But you are not parsing this file with your code. You are doing something which makes no sense at all. You call LoadXml not with this file. You try to parse just the line, which the string str calculated from reply, by removing some hard-coded strings. This is not XML at all. No wonder, XML parser cannot parse some text which is not XML.

What to do? Parse the whole file first (in your case, from stream, XmlDocument.Load Method (Stream) (System.Xml)), than change what you want.

You should never read lines from stream or file, to parse XML, unless you want to create your own parser, which hardly may make any practical sense.

—SA
 
Share this answer
 
v4
Comments
Member 10261487 17-Jun-16 3:47am    
Actually there is no any xml file in this situation. I will get only String Stream which is look as like xml. So, how can I get my targeted value from this string?
Sergey Alexandrovich Kryukov 17-Jun-16 4:08am    
I know, this is taken into account in my answer. Please read it more thoroughly. It makes no difference, file or stream. I provided a link to the Load method using stream.
—SA
Member 10261487 17-Jun-16 4:43am    
But, here you cannot use Load method cause I am working with a string. You can use Load Method only if you have xml file. But, when you will use String and the string format is as like XML than you have to use LoadXML() method. https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml(v=vs.110).aspx

Now, I got the correct String. And getting new Exception: Object Reference is not set.

My Code:
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());
}
Sergey Alexandrovich Kryukov 17-Jun-16 9:12am    
I'm reading your first line of your comment. The reply: not true.
—SA
I have solved it.

My Code:
C#
XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(str);
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("file-nb-frames");
            string file_nb_frames = string.Empty;
            foreach (XmlNode node in nodeList)
            {
                file_nb_frames = node.InnerText;
            }

            Console.WriteLine(file_nb_frames);


This is working now. Thank you all.
 
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