Home  >  Article  >  Backend Development  >  XML (4) XDocument and XmlDocument search for specified nodes

XML (4) XDocument and XmlDocument search for specified nodes

黄舟
黄舟Original
2017-02-10 16:32:572627browse

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>

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>

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

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