【问题标题】:Want to return the entire contents of an XElement as a string [duplicate]想要将 XElement 的全部内容作为字符串返回 [重复]
【发布时间】:2008-12-06 22:21:47
【问题描述】:

我是 C# 中 LINQ 的新手,我正在使用它来读取和处理 XML 文件。

我能够上下导航我的元素,但我想要的某些元素是将 entire 内容作为字符串返回。意思是,我有一个这样的元素:

<element1>
    <subel1> some text here </subel1>
</element1>

当我得到 element1 的值时,我想将整个内容作为文本字符串取回,如下所示:"&lt;subel1&gt; some text here &lt;/subel1&gt;"

这有意义吗?有人可以帮忙吗?

(作为 here 的副本关闭)

【问题讨论】:

    标签: c# xml linq xelement


    【解决方案1】:

    (编辑:回复后,我通过搜索"XElement InnerXml"找到了完全相同的副本)

    对于XmlDocument,这只是.InnerXml - 不过,不确定XElement。也许作弊?

        StringBuilder sb = new StringBuilder();
        foreach (var el in foo.Nodes()) sb.AppendLine(el.ToString());
        string s = sb.ToString();
    

    【讨论】:

    • 效果很好,谢谢! -Adeena
    猜你喜欢
    • 2021-10-19
    • 2017-09-30
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    相关资源
    最近更新 更多