我有一个 xml 文件,我使用 LINQ to XML 从中提取 html。这是该文件的示例:
<?xml version="1.0" encoding="utf-8" ?>
<tips>
<tip id="0">
This is the first tip.
</tip>
<tip id="1">
Use <b>Windows Live Writer</b> or <b>Microsoft Word 2007</b> to create and publish content.
</tip>
<tip id="2">
Enter a <b>url</b> into the box to automatically screenshot and index useful webpages.
</tip>
<tip id="3">
Invite your <b>colleagues</b> to the site by entering their email addresses. You can then share the content with them!
</tip>
</tips>
我使用以下查询从文件中提取“提示”:
Tip tip = (from t in tipsXml.Descendants("tip")
where t.Attribute("id").Value == nextTipId.ToString()
select new Tip()
{
TipText= t.Value,
TipId = nextTipId
}).First();
我遇到的问题是 Html 元素被删除。我希望使用像 InnerHtml 这样的东西来代替 Value,但这似乎不存在。
有任何想法吗?
预先感谢大家,
Dave
Call t.ToString()
代替Value
。这将以字符串形式返回 XML。您可能需要使用 SaveOptions 的重载来禁用格式。我现在无法检查,但我怀疑它会包含元素标签(和元素),因此您需要将其删除。
请注意,如果您的 HTML 不是有效的 XML,您最终会得到一个无效的整体 XML 文件。
XML 文件的格式是否完全超出您的控制范围?如果内部的任何 HTML 都采用 XML 编码,那就更好了。
编辑:避免获取外部部分的一种方法可能是执行类似的操作(当然,在从查询调用的单独方法中):
StringBuilder builder = new StringBuilder();
foreach (XNode node in element.Nodes())
{
builder.Append(node.ToString());
}
这样您将获得 HTML 元素及其后代和散布的文本节点。我强烈怀疑它基本上相当于 InnerXml。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)