获取HTML标签嵌入在使用XML LINQ [英] Get html tags embedded in xml using linq

查看:106
本文介绍了获取HTML标签嵌入在使用XML LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的XML文件,它看起来像这样。

I have a basic xml file that looks like this.

    <root>
     <item>
        <title><p>some title</p></title>
     </item>
    ...
    </root>



我要的,是让包括使用LINQ的XML的HTML标记的整个标题字符串在一个中继器显示它。
我可以得到没有问题的称号,但< P> 标签被剥离出来

如果我用

标题= item.Element(题)。的ToString(),它的工作原理,但不知何故,我得到的所有。XML标记,以及 - 这意味着标题不显示在HTML

If I use
title = item.Element("title").ToString(), it works somehow but I get all the xml tag as well - meaning the title is not displayed in html.

我已经尝试过用编码<&放大器; LT;,但要做到这一点,使XML难以阅读

I already tried with encoding the "<" with "&lt;" but to do this makes the xml hard to read.

会是什么。是除了使用CDATA和编码一个可能的解决方案?

What would be a possible solution besides using CDATA and encoding?

干杯
特里

推荐答案

从标题元素创建一个阅读器和阅读InnerXml:

Create a reader from the title element and read InnerXml:

    static void Main(string[] args)
    {
        string xml = "<root><item><title><p>some title</p></title></item></root>";

        XDocument xdoc = XDocument.Parse(xml);
        XElement te = xdoc.Descendants("title").First();
        using (XmlReader reader = te.CreateReader())
        {
            if (reader.Read())
                title = reader.ReadInnerXml();
        }
    }

这篇关于获取HTML标签嵌入在使用XML LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆