XmlNode.OwnerDocument.ChildNodes为空

时间:2015-09-28 14:09:29

标签: c# xml xmldocument

我从Web服务获取XmlElement。我得到了一些意想不到的东西,因为xmlElement.OwnerDocument.ChildNodes是空的。怎么可能?

这是xml:

<tns1:VideoSource xmlns:tns1="http://www.onvif.org/ver10/topics">
   <MotionAlarm wstop:topic="true" xmlns:wstop="http://docs.oasis-open.org/wsn/t-1" xmlns="http://www.onvif.org/ver10/events/wsdl">
   </MotionAlarm>
</tns1:VideoSource>

1 个答案:

答案 0 :(得分:0)

我用下面的代码测试了你的xml,还有像你这样的孩子。我怀疑可能有一些白色字符正在创建错误。如果您从网站(可能是流)获取数据,则流的末尾可能会有一些不可见的空字符。确保您的流类使用UTF8编码。某些流中的默认编码是Ascii,它可以更改字符并添加填充字符,这可能会产生问题。

           string input =
                "<tns1:VideoSource xmlns:tns1=\"http://www.onvif.org/ver10/topics\">" +
                  "<MotionAlarm wstop:topic=\"true\" xmlns:wstop=\"http://docs.oasis-open.org/wsn/t-1\" xmlns=\"http://www.onvif.org/ver10/events/wsdl\">" +
                  "</MotionAlarm>" +
                "</tns1:VideoSource>";

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(input);
            XmlNodeList videoSource = doc.ChildNodes;
            XmlNodeList motionAlarm = videoSource[0].ChildNodes;​