开发者

i mean extracting the word between two tabs [duplicate]

开发者 https://www.devze.com 2023-01-13 14:14 出处:网络
This question already has an answer here: Closed 12 years ago. Possible Dupli开发者_Python百科cate:
This question already has an answer here: Closed 12 years ago.

Possible Dupli开发者_Python百科cate:

extracting the letter in between tags

okie let me give an example we have a file,where i need to open it in c# and scan and extract information ,like i want hi to be extracted which is between tag so only i want that to be extracted and copied to other file ,so what can i do??....and how to start about

<REFER> abcd</REFER>
<BODY>hi</BODY>
<p1>hello</p1>


You probably want to use an HTML-Parser (pick one) and then use it to retrieve the content between the tags.


Well, I'd start with looking in the System.IO namespace in order to learn how to read and write files...

Your data looks like it may be XML, so look at the XmlDocument class in System.Xml or the Linq XDocument class. If it's not XML then you're going to have to parse it yourself, so read up on the String class.


Well, this may be a trivial example, but if your document structure gets any more complex than this, I'd highly recommend HtmlAgilityPack.

For the example given, you'd use it like this:

string html = "<REFER> abcd</REFER><BODY>hi</BODY><p1>hello</p1>";

var doc = new HtmlDocument();

doc.LoadHtml(html);

HtmlNode root = doc.DocumentElement;
HtmlNode body = root.SelectSingleNode("BODY");

string extracted = body.InnerText;

That may seem like overkill; but like I said, if the document structure gets any more complex (I can't imagine that the documents you'll be parsing really look like the example), you'll be glad you did it.

0

精彩评论

暂无评论...
验证码 换一张
取 消