如何根据 div id 子串 html 字符串
How to substring html string based on div id
如果我有一个 html 格式的字符串,如下所示:
<p style="text-align:center"> </p>
<p style="text-align:center"> </p>
<p style="text-align:center"><strong>To The ‎<span>Embassy of The United Kingdom</span>‏</strong></p>
<p>The ORG- certifies that ‎<strong><span>Mrs.</span></strong>‏‎<strong> </strong>‏‎<strong><span>Matilda Johan</span></strong>‏,</p>
<p>has been‎‏‎‏ working since <strong><span>01/10/2003</span></strong>‏ until present.</p>
<p>‎<span>Presently, she is working as</span>‏‎‏‎ a / an ‎<strong><span>JOB TITLE NOT DEFINED</span></strong>‏ at <strong><span>Dean of the Faculty of Engineering and Technology Office - College of Engineering and Technology - S</span></strong>‏-‎‏‎‎<strong><span>College of Engineering and Technology </span></strong>‏.</p>
<p><strong>This certificate was issued upon </strong>‎<strong><span>her request</span></strong>‏ <strong>and without any commitment on behalf of the ORG.</strong></p>
<div>
<div id="dv_sign_en" style="float:left;clear:both;font-style: italic;">...</div>
<div></div>
</div>
如何仅在 id 以 dv_sign_
开头的 div 的父 div 之前获取字符串,因此结果将是:
<p style="text-align:center"> </p>
<p style="text-align:center"> </p>
<p style="text-align:center"><strong>To The ‎<span>Embassy of The United Kingdom</span>‏</strong></p>
<p>The ORG- certifies that ‎<strong><span>Mrs.</span></strong>‏‎<strong> </strong>‏‎<strong><span>Matilda Johan</span></strong>‏,</p>
<p>has been‎‏‎‏ working since <strong><span>01/10/2003</span></strong>‏ until present.</p>
<p>‎<span>Presently, she is working as</span>‏‎‏‎ a / an ‎<strong><span>JOB TITLE NOT DEFINED</span></strong>‏ at <strong><span>Dean of the Faculty of Engineering and Technology Office - College of Engineering and Technology - S</span></strong>‏-‎‏‎‎<strong><span>College of Engineering and Technology </span></strong>‏.</p>
<p><strong>This certificate was issued upon </strong>‎<strong><span>her request</span></strong>‏ <strong>and without any commitment on behalf of the ORG.</strong></p>
String html;
using (StreamReader reader = new StreamReader($@"D:\OneDrive\Dokumentumok\Projects\html.txt")) {
html = reader.ReadToEnd();
}
Int32 index = html.IndexOf("<div id=\"dv_sign_");
html = html.Substring(0, index);
index = html.LastIndexOf("<div>");
html = html.Substring(0, index);
如果我有一个 html 格式的字符串,如下所示:
<p style="text-align:center"> </p>
<p style="text-align:center"> </p>
<p style="text-align:center"><strong>To The ‎<span>Embassy of The United Kingdom</span>‏</strong></p>
<p>The ORG- certifies that ‎<strong><span>Mrs.</span></strong>‏‎<strong> </strong>‏‎<strong><span>Matilda Johan</span></strong>‏,</p>
<p>has been‎‏‎‏ working since <strong><span>01/10/2003</span></strong>‏ until present.</p>
<p>‎<span>Presently, she is working as</span>‏‎‏‎ a / an ‎<strong><span>JOB TITLE NOT DEFINED</span></strong>‏ at <strong><span>Dean of the Faculty of Engineering and Technology Office - College of Engineering and Technology - S</span></strong>‏-‎‏‎‎<strong><span>College of Engineering and Technology </span></strong>‏.</p>
<p><strong>This certificate was issued upon </strong>‎<strong><span>her request</span></strong>‏ <strong>and without any commitment on behalf of the ORG.</strong></p>
<div>
<div id="dv_sign_en" style="float:left;clear:both;font-style: italic;">...</div>
<div></div>
</div>
如何仅在 id 以 dv_sign_
开头的 div 的父 div 之前获取字符串,因此结果将是:
<p style="text-align:center"> </p>
<p style="text-align:center"> </p>
<p style="text-align:center"><strong>To The ‎<span>Embassy of The United Kingdom</span>‏</strong></p>
<p>The ORG- certifies that ‎<strong><span>Mrs.</span></strong>‏‎<strong> </strong>‏‎<strong><span>Matilda Johan</span></strong>‏,</p>
<p>has been‎‏‎‏ working since <strong><span>01/10/2003</span></strong>‏ until present.</p>
<p>‎<span>Presently, she is working as</span>‏‎‏‎ a / an ‎<strong><span>JOB TITLE NOT DEFINED</span></strong>‏ at <strong><span>Dean of the Faculty of Engineering and Technology Office - College of Engineering and Technology - S</span></strong>‏-‎‏‎‎<strong><span>College of Engineering and Technology </span></strong>‏.</p>
<p><strong>This certificate was issued upon </strong>‎<strong><span>her request</span></strong>‏ <strong>and without any commitment on behalf of the ORG.</strong></p>
String html;
using (StreamReader reader = new StreamReader($@"D:\OneDrive\Dokumentumok\Projects\html.txt")) {
html = reader.ReadToEnd();
}
Int32 index = html.IndexOf("<div id=\"dv_sign_");
html = html.Substring(0, index);
index = html.LastIndexOf("<div>");
html = html.Substring(0, index);