本文介绍了如何获取下面的Xml文件中的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如何获取以下Xml文件中的节点? <? xml version = 1.0 encoding = utf-8 ? > - < xd:xmldiff version = 1.0 srcDocHash = 4685481238288745685 options = IgnoreChildOrder IgnoreNamespaces IgnorePI IgnorePrefixes 片段 = no xmlns:xd = http:// schemas.microsoft.com/xmltools/2002/xmldiff\"> - < xd:node 匹配 = 1 > - < xd:add > < 年龄 > 12 < /年龄 > < 电话 > 12345678 < /电话 > < 电子邮件 > [email protected] < /电子邮件 > < 地址 > qwqw < / address > < / xd:add > < / xd:node > < / xd:xmldiff > 我想要获取节点年龄/电话/电子邮件/地址: string childnode = string .Empty; XmlDocument docXml = new XmlDocument(); docXml.Load(diffXml); var nsmgr = new XmlNamespaceManager(docXml.NameTable); nsmgr.AddNamespace( xd, http://schemas.microsoft.com/xmltools/2002/xmldiff); var nl = docXml.SelectNodes( // xd:add / @ href,nsmgr); // var nl = docXml.SelectNodes(// xd:add,nsmgr); // 获取所有玩家节点的列表 // 定义单个节点 XmlNode节点; // 获取根Xml元素 XmlElement root = docXml.DocumentElement; for ( int i = 0 ; i < root.ChildNodes.Count; i ++) { if ( string .IsNullOrEmpty(childnode)== true ) { childnode = root.Name; } else { childnode + = , + root.Name; } } 解决方案 尝试使用XmlNode.SelectSingleNode方法: XmlDocument docXml = new XmlDocument(); docXml.Load(diffXml); var nsmgr = new XmlNamespaceManager(docXml.NameTable); nsmgr.AddNamespace( xd, http://schemas.microsoft.com/xmltools/2002/xmldiff); XmlElement root = docXml.DocumentElement; XmlNode node = root.SelectSingleNode( 年龄,nsmgr); Console.WriteLine(node.InnerXml); XmlNode node = root.SelectSingleNode( Phone,nsmgr); Console.WriteLine(node.InnerXml); // ......等等...... How to get the nodes in the below Xml file?<?xml version="1.0" encoding="utf-8" ?>- <xd:xmldiff version="1.0" srcDocHash="4685481238288745685" options="IgnoreChildOrder IgnoreNamespaces IgnorePI IgnorePrefixes" fragments="no" xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff">- <xd:node match="1">- <xd:add> <Age>12</Age> <Phone>12345678</Phone> <Email>[email protected]</Email> <address>qwqw</address> </xd:add> </xd:node> </xd:xmldiff>I want to fetch the nodes Age/Phone/Email/address:string childnode = string.Empty;XmlDocument docXml = new XmlDocument();docXml.Load(diffXml);var nsmgr = new XmlNamespaceManager(docXml.NameTable);nsmgr.AddNamespace("xd", "http://schemas.microsoft.com/xmltools/2002/xmldiff");var nl = docXml.SelectNodes("//xd:add/@href", nsmgr);//var nl = docXml.SelectNodes("//xd:add", nsmgr);// Get a list of all player nodes// Define a single nodeXmlNode node;// Get the root Xml elementXmlElement root = docXml.DocumentElement;for (int i = 0; i < root.ChildNodes.Count; i++){ if (string.IsNullOrEmpty(childnode) == true) { childnode = root.Name; } else { childnode += "," + root.Name; }} 解决方案 Try it with XmlNode.SelectSingleNode Method :XmlDocument docXml = new XmlDocument();docXml.Load(diffXml); var nsmgr = new XmlNamespaceManager(docXml.NameTable);nsmgr.AddNamespace("xd", "http://schemas.microsoft.com/xmltools/2002/xmldiff");XmlElement root = docXml.DocumentElement;XmlNode node = root.SelectSingleNode("Age", nsmgr);Console.WriteLine(node.InnerXml);XmlNode node = root.SelectSingleNode("Phone", nsmgr);Console.WriteLine(node.InnerXml);// ... and so long ... 这篇关于如何获取下面的Xml文件中的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 04:33