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 ();
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.
XDocument document = XDocument.Load("List1.xml"); XElement rootElement = document.Root; IEnumerable 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"; }
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 (m.sbmmt.com)!