我想提取<P>
标记下的前两句话。
例如(输入字符串):
<P align=justify><STRONG>Pricings<BR></STRONG>It was another active week for names leaving the database. The week's prints consisted of two ILS, and sever ITS.</P>
必需的输出字符串:
It was another active week for names leaving the database. The week's prints consisted of two ILS, and sever ITS.
目前,我的功能如下:
System.Xml.XmlException:'justify'是一个意外的令牌。预期的标记是“”或“
price = bottom.Substring(bottom.IndexOf("Pricings"), 8);
XmlDocument doc = new XmlDocument();
doc.LoadXml(bottom);
XmlNodeList pList = doc.SelectNodes("/P[@align='justify']/strong");
foreach (XmlNode pValue in pList)
{
string innerText = pValue.ChildNodes[0].InnerText;
innerText = result;
}
我不清楚,如何解决这个问题。感谢您的进一步帮助。
答案 0 :(得分:2)
它不是XML字符串,而是HTML字符串。
由于HTML本身通常不能很好地形成(在你的情况下它的格式不正确),通常你不能使用XML解析器来解析HTML。
相反,您可以使用HTML Agility Pack(推荐方式),或使用正则表达式解析此文本(通常不推荐,但有时可能)。
以下是如何使用HtmlAgility包获取数据的示例代码:
var s = "<P align=justify><STRONG>Pricings<BR></STRONG>It was another active week for names leaving the database. The week's prints consisted of two ILS, and sever ITS.</P>";
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(s);
string result;
var p = doc.DocumentNode.SelectSingleNode("p");
if (p.ChildNodes.Count == 2)
result = p.ChildNodes[1].InnerText;
注意:Html Agility包也可以在Visual Studio中作为NuGet包使用。
答案 1 :(得分:1)
我只是在php / magento中做,试试这个来解决。
$xml = simplexml_load_file("../app/etc/local.xml") or die("X");$host = $xml->xpath('global/resources/default_setup/connection/host');$host = $host[0][0];$usernm = $xml->xpath('global/resources/default_setup/connection/username');$usernm = $usernm[0][0];$pwd = $xml->xpath('global/resources/default_setup/connection/password');$pwd = $pwd[0][0];$db = $xml->xpath('global/resources/default_setup/connection/dbname');$db = $db[0][0];$link = mysql_connect($host, $usernm, $pwd);
If (!$link) { die ('Could not connect: ' . mysql_error()); }
mysql_select_db($db) or die ('Unable to select database');
$result = mysql_query("SELECT * FROM catalog_product_flat_1 Where shipping_price IS NULL AND type_id='simple'");
$noOfRecord = mysql_num_rows($result);
我使用xml文件作为magento local.xml文件,该文件位于magento / app / etc / local.xml ..