如何遍历XDocument的节点我正在尝试遍历我的xml文档的节点,获取每个节点中Ed的值。我使用Linq首先对XDocument进行排序,然后尝试遍历节点。我似乎无法找到正确的foreach循环来实现这一点。任何帮助表示赞赏。vardoc=XDocument.Load("files\config.xml");varnewDoc=newXDocument(newXElement("Config",frompindoc.Element("Config").Elements("Profile")orderbyint.Parse(p.Element("order").Value)选择p));foreach(XElementxeinnewDoc.Nodes()){MessageBox.Show(xe.Element("username").Value);}//XML文档ScopeScope1...0000ScopeW2BN[IP]Lobby12Scope2Scope2...0000ScopeW2BN[IP]Lobby11试试这个。不确定为什么需要第二个文档。foreach(XElementxeindoc.Descendants("Profile")){MessageBox.Show(xe.Element("username").Value);}使用XPathDocument和XPath表达式更容易。vardoc=newXPathDocument("files\config.xml")foreach(varusernameindoc.CreateNavigator().Select("//username"){...}如果您正在寻找内部节点,即递归或其他,你可以检查元素是否有元素。例如,您从数据库中读取xml。以上就是C#学习教程:如何遍历XDocument的节点共享的所有内容。如果对大家有用,需要详细了解C#学习教程,希望大家多多关注——stringxmlRoot="selectXmlItemfromdb";XDocumentdoc=XDocument.Parse(xmlRoot);列表xElementList=doc.Descendants().Tolist();foreach(XElementelementinxElementList){//读取元素并处理你的节点if(element.HasElements){//这里你可以到达嵌套节点}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
