【问题标题】:How to extract specific value from xml string?如何从 xml 字符串中提取特定值?
【发布时间】:2015-04-16 09:17:37
【问题描述】:

我想提取<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;
}

我不太清楚,如何解决这个问题。感谢您提供任何进一步的帮助。

【问题讨论】:

  • 您的 HTML 不是有效的 XML 字符串。无法使用 XmlDocument 加载。

标签: c# xml xmldocument


【解决方案1】:

它不是 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 包提供。

【讨论】:

  • 非常感谢您的大力帮助。
【解决方案2】:

我只是在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/app/etc/local.xml.. 的 magento local.xml 文件。

【讨论】:

  • 您的答案看起来与问题完全无关。 OP的问题与您回答中提到的php、magento、mysql和其他内容无关。您是否错过了要回答的问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多