本文介绍了未在xmlnode.selectsinglenode()中设置对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试从XML格式的字符串中获取特定字段的值。 我的代码: 静态 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); 尝试 { foreach (XmlNode childrenNode_frame nodes_frame) { Console.WriteLine( Frames: + childrenNode_frame.SelectSingleNode( // file-nb-frames )。Value.ToString()+ 帧率: + childrenNode_frame.SelectSingleNode( // fps)。Value.ToString()); } } catch (异常错误) { Console.WriteLine(err.ToString( )); } Console.Read(); } 现在获取异常对象重新引用未在 Console.WriteLine( 框架: + childrenNode_frame.SelectSingleNode( // file-nb-frames)。Value.ToString()+ 帧速率: + childrenNode_frame.SelectSingleNode( // fps)。Value.ToString()); 这一行。 请帮助我。提前致谢。 我的尝试: 我有使用Load()方法。比我还有另外一个例外:附加信息:路径中的非法字符。解决方案 替换: 。Value.ToString() 使用: 。InnerText I am trying to get values from specific fields from a string which is XML format. My Code: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 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." 解决方案 Replace:.Value.ToString()With:.InnerText 这篇关于未在xmlnode.selectsinglenode()中设置对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 13:22