Home > Article > Backend Development > XML (4) XDocument and XmlDocument search for specified nodes
<span style="font-family:Microsoft YaHei;font-size:18px;"> StringBuilder str = new StringBuilder(); XmlDocument document = new XmlDocument(); document.Load("List1.xml"); XmlNodeList nodelist = document.GetElementsByTagName("person"); foreach (XmlNode item in nodelist) { str.Append(item.FirstChild.InnerText.ToString()); str.Append(" "); } textBox1.Text = str.ToString ();</span>
You can get the specified name or specified ID through the document. The above is to get the specified name. Then output the contents of the first child node under the name node by traversing.
<span style="font-family:Microsoft YaHei;font-size:18px;"> XDocument document = XDocument.Load("List1.xml"); XElement rootElement = document.Root; IEnumerable<XElement> ie = rootElement.Descendants("person").Where(x => Convert.ToInt32(x.Attribute("id").Value) > 1); foreach (var item in ie) { textBox1.Text += item.Attribute("id").Value+"\t"; }</span>
Through XDocument, we can query the data we need more conveniently and quickly. Which can be combined with lambda expressions for retrieval.
The above is the content of XML (4) XDocument and XmlDocument searching for the specified node. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!