<?xml version="1.0" encoding="utf-8" ?>
<config>
<conn>
<str>sssssssssss</str>
<title>aaaaaaaaa</title>
</conn>
</config>有这样一个文档,名字为config.xml,请大家给出读取str和title的值的代码。谢谢,最好能有注释。

解决方案 »

  1.   

    [C#] 
    using System;
    using System.IO;
    using System.Xml;public class Sample
    {
      public static void Main()
      {
        //Create the XmlDocument.
        XmlDocument doc = new XmlDocument();
        doc.Load("books.xml");    //Display all the book titles.
        XmlNodeList elemList = doc.GetElementsByTagName("title");
        for (int i=0; i < elemList.Count; i++)
        {   
          Console.WriteLine(elemList[i].InnerXml);
        }    }
    }
    该示例将 books.xml 文件用作输入。
    <?xml version='1.0'?>
    <!-- This file represents a fragment of a book store inventory database -->
    <bookstore>
      <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
          <first-name>Benjamin</first-name>
          <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
      </book>
      <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
          <first-name>Herman</first-name>
          <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
      </book>
      <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
          <name>Plato</name>
        </author>
        <price>9.99</price>
      </book>
    </bookstore>
      

  2.   

    XmlDocument document = new XmlDocument();
    document.Load(this.nPath);
    XmlNode root = document.FirstChild;
    foreach(XmlNode child in root.ChildNodes)
    {
    if(child.Name == "ServerName")
    {
    this.Path = child.InnerText.ToString();
    }
    if(child.Name == "WebUrl")
    {
    this.WebUrl = child.InnerText.ToString();
    }
    }
      

  3.   

    XmlDocument docXml = new XmlDocument();
    docXml.Load("config.xml");
    XmlNodeList configStr = doc.GetElementsByTagName("title");