Home > Backend Development > XML/RSS Tutorial > XML (4) XDocument and XmlDocument search for specified nodes

XML (4) XDocument and XmlDocument search for specified nodes

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

XmlDocument

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


<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>
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 (www.php.cn)!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template