如何解析XML文件

时间:2009-10-11 02:51:50

标签: c# .net xml

我有一个XML文件,如下所示:

  <results>
    <result>
      <title>Welcome+to+The+JASON+Project%21</title>
      <url>http%3A%2F%2Fwww.jason.org%2F</url>
      <domain />
      <inside_links>
        <inside_link>
          <description>News</description>
          <url>http%3A%2F%2Fwww.jason.org%2FPublic%2FNews%2FNews.aspx</url>
        </inside_link>
        <inside_link>
          <description>register</description>
          <url>http%3A%2F%2Fwww.jason.org%2Fpublic%2Fregistration%2Fregistration.aspx</url>
        </inside_link>
        <inside_link>
          <description>Argonauts</description>
          <url>http%3A%2F%2Fwww.jason.org%2FPublic%2FArgonauts%2FArgonauts.aspx</url>
        </inside_link>
        <inside_link>
          <description>Curriculum</description>
          <url>http%3A%2F%2Fwww.jason.org%2FPublic%2FCurriculum%2FCurriculum.aspx</url>
        </inside_link>
        <inside_link>
          <description>Credits</description>
          <url>http%3A%2F%2Fwww.jason.org%2Fpublic%2FMisc%2FCredits.aspx</url>
        </inside_link>
      </inside_links>
      <inside_keywords>National+Science+Education+Standards, National+Geographic+Society, Physical+Science, Professional+Development, Earth+Science</inside_keywords>
    </result>
  </results>

......我对如何阅读而感到困惑。我只想将标题,描述和URL分成不同的字符串。类似的东西:

foreach line in lines
string title = gettitle;
string description = getdescription;
string url = geturl;

...我已经阅读了很多教程,但所有这些教程似乎与我需要做的事情无关。有人可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:6)

如果您使用的是.NET 3.5,我建议使用LINQ to XML ...

XDocument doc = XDocument.Load(filename);
XElement insideLinks = doc.Root.Element("result").Element("inside_links");
foreach (XElement insideLink in insideLinks.Elements())
{
    string description = (string)insideLink.Element("description");
    string url = (string)insideLink.Element("url");
}

这也允许您使用内置的“查询”语法,因此您可以执行类似的操作......

XDocument doc = XDocument.Load(filename);
XElement insideLinks = doc.Root.Element("result").Element("inside_links");
var allTitles = from XElement insideLink 
                in insideLinks.Elements("inside_link")
                select (string)insideLink.Element("title");

(根据评论编辑)

答案 1 :(得分:5)

要扩展LINQ to XML建议,可以使用select子句创建对象来表示已解析的链接:

XDocument doc = XDocument.Load(filename);
var links = from link in doc.Descendants("inside_link")
            select new
            {
                Description = (string)link.Element("description"),
                Url = HttpUtility.UrlDecode((string)link.Element("url"))
            };

foreach(var l in links)
    Console.WriteLine("<a href=\"{0}\">{1}</a>", l.Url, l.Description);

在这种情况下,links将是具有DescriptionUrl属性的匿名类型的对象序列,其中Url已解码。这个foreach会显示如下内容:

<a href="http://www.jason.org/Public/News/News.aspx">News</a>
<a href="http://www.jason.org/public/registration/registration.aspx">register</a>
...

答案 2 :(得分:2)

试试这个:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("yourfile.xml");
foreach (XmlNode result in xmlDoc.SelectNodes("/results/result"))
{
    string title = result.SelectSingleNode("title").InnerText;
    string url = result.SelectSingleNode("url").InnerText;
    foreach (XmlNode insideLink in result.SelectNodes("inside_links/inside_link"))
    {
        string description = insideLink.SelectSingleNode("description").InnerText;
    }
}