XML解析时获取到的节点为null

XML内容如下:


            	
            	
            	
            	



解析代码如下:

            byte[] data = Encoding.UTF8.GetBytes(msg);

            System.IO.MemoryStream ms = new System.IO.MemoryStream(data);

            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

            try
            {
                xmlDoc.Load(ms);
            }
            catch (Exception ex)
            {
                throw new Tsxd.ExceptionProcessor.ExtensibleException("解析返回结果错误:" + ex.Message);
            }

            System.Xml.XPath.XPathNavigator xpnRoot = xmlDoc.CreateNavigator();
            XPathNavigator xpnBSXml = xpnRoot.SelectSingleNode("PRPM_IN401030UV01");
            

解析时xpnRoot为null,因为XML中有自定义的命名空间,需要用以下方式解析:

  byte[] data = Encoding.UTF8.GetBytes(msg);

            System.IO.MemoryStream ms = new System.IO.MemoryStream(data);

            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

            try
            {
                xmlDoc.Load(ms);
            }
            catch (Exception ex)
            {
                throw new Tsxd.ExceptionProcessor.ExtensibleException("解析返回结果错误:" + ex.Message);
            }

            XmlNamespaceManager xmlnsm = new XmlNamespaceManager(xmlDoc.NameTable);
            xmlnsm.AddNamespace("ns", "urn:hl7-org:v3");
            XmlNode ret = xmlDoc.SelectSingleNode("/ns:PRPM_IN401030UV01/ns:id", xmlnsm); 

ret.InnerText即为id节点的值。

你可能感兴趣的:(xml,c#)