缺少Xmldocument.loadxml根元素 [英] Xmldocument.loadxml root element is missing

查看:270
本文介绍了缺少Xmldocument.loadxml根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取xml格式的流来检索特定的字段值。



我的代码:

I am trying to read a stream which is in xml format to retrieve specific field values.

My Code:

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





现在我收到异常缺少根元素;



在我的控制台中,xml字符串如下所示:



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

In my console the xml string is as like this:

<?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>





我的尝试:



我尝试过使用这个命令:

var reply = reader.ReadToEnd();



我的控制台里只有黑色的窗口。



What I have tried:

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

Than there is only black window in my console.

推荐答案

Richard Deeming是对的 - 请看他对这个问题的评论。



是的,你的XML文件是有效的。如果你解析它,它将完美解析。



但是你没有用你的代码解析这个文件。你正在做一些毫无意义的事情。您不使用此文件调用 LoadXml 。您尝试通过删除一些硬编码字符串来解析从回复计算的字符串 str 的行。 这根本不是XML。难怪,XML解析器无法解析一些非XML的文本。



怎么办?首先解析整个文件(在您的情况下,从流, XmlDocument.Load方法(流)(System.Xml)),而不是改变你想要的东西。



你永远不应该从流或文件中读取行来解析XML ,除非你想创建自己的解析器,这几乎没有任何实际意义。



-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

我已经解决了。



我的代码:

I have solved it.

My Code:
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.


这篇关于缺少Xmldocument.loadxml根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆