1 /// <summary> 2 /// xml字符串转xml文档 忽略注释信息 3 /// </summary> 4 /// <param name="sXml">xml字符串</param> 5 /// <returns></returns> 6 public XmlDocument XmlFromStrIgnoreComment(string sXml) 7 { 8 try 9 { 10 XmlDocument _XmlDocument = new XmlDocument(); 11 _XmlDocument.LoadXml(sXml); 12 return this.XmlRemoveComment(_XmlDocument); 13 } 14 catch (Exception) 15 { 16 return null; 17 } 18 19 } 20 /// <summary> 21 /// xml文档移除 注释节点 22 /// </summary> 23 /// <param name="sXml">xml文档</param> 24 /// <returns></returns> 25 public XmlDocument XmlRemoveComment(XmlDocument sXml) 26 { 27 List<XmlNode> dXmlNodeList = new List<XmlNode>(); 28 this.GetXmlRemoveComment(sXml.ChildNodes, dXmlNodeList); 29 foreach (XmlNode vXmlNode in dXmlNodeList) 30 { 31 vXmlNode.ParentNode.RemoveChild(vXmlNode); 32 } 33 return sXml; 34 } 35 36 /// <summary> 37 /// 获取xml中的 注释节点 38 /// </summary> 39 /// <param name="xmlNodeList">待查找结点</param> 40 /// <param name="dXmlNodeList">注释节点</param> 41 public void GetXmlRemoveComment(XmlNodeList xmlNodeList, List<XmlNode> dXmlNodeList) 42 { 43 foreach (XmlNode xmlNode in xmlNodeList) 44 { 45 if (xmlNode.NodeType == XmlNodeType.Comment) 46 { 47 dXmlNodeList.Add(xmlNode); 48 } 49 else 50 { 51 if (xmlNode.HasChildNodes) 52 { 53 GetXmlRemoveComment(xmlNode.ChildNodes, dXmlNodeList); 54 } 55 } 56 } 57 }