1

我有这样的html代码:

<div class="topright">
    <a href="" TARGET="_parent" title="News Letter" >
       <img border="none" src="/images/newsletter-button.gif' alt="News Letter" />
     </a>
</div>

我正在 C# 中开发函数 findvalue:

if (line.Contains("title"))
{
    sTitle = findvalue("title", line);
}

findvalue函数是简单的字符串操作函数,其工作方式类似于此行<a href="" TARGET="_parent" title="News Letter" > find 函数将返回 News Letter 值,同样我将获取其他属性的值,如 href、src atc

4

2 回答 2

1

您可以使用与 XML 模式相关的类。

using System;
using System.IO;
using System.Xml;

public class Sample
{

    public static void Main()
    {

        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<input title='Meluha' Value='1-861001-75-7'>" +
                    "<title>The Imortals of Meluha</title>" +
                    "</input>");

        XmlNode root = doc.DocumentElement;

        XmlNode value = doc.SelectNodes("//input/@value")[0];

        Console.WriteLine("Inner text: " + value.InnerText);

    XmlNode value = doc.SelectNodes("//title/@value")[0];

    Console.WriteLine("Title text: " + value.InnerText);
     } 
}
于 2013-04-19T05:37:05.663 回答
0

更好的是尝试使用 HtmlAgilityPack 而不浪费时间和精力,谢谢你们......

于 2013-04-19T08:55:12.007 回答