XML (4) XDocument and XmlDocument search for specified nodes

黄舟
Release: 2017-02-10 16:32:57
Original
2556 people have browsed it

XmlDocument

 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 ();
Copy after login

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


 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"; }
Copy after login

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)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!