首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XElement从StreamReader文本创建新的StreamReader

使用XElement从StreamReader文本创建新的StreamReader
EN

Stack Overflow用户
提问于 2013-10-18 19:56:47
回答 2查看 3.8K关注 0票数 2

我试图将现有的XML文件“嵌入”到另一个“根”XML文件的另一个节点中。假设有一个通往现有xml文件的路径.

代码语言:javascript
复制
 StreamReader reader = new StreamReader(path);

    string lines = reader.ReadLine();
    while (!reader.EndOfStream)
    {
        lines = reader.ReadLine();
    }

    XDocument doc = new XDocument(
        new XComment("You can copy and paste or open the XML in Excel."),
        new XElement("Root",
            new XElement("logs",lines))
    );

我的结局是这样的:

<Root><logs>&lt;log&gt;&lt;username&gt;otonomy&lt;/

一些解码程序需要帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-18 20:02:27

使用XElement.Load()静态方法代替:

代码语言:javascript
复制
XDocument doc = new XDocument(
        new XComment("You can copy and paste or open the XML in Excel."),
        new XElement("Root",
            new XElement("logs"
                XElement.Load(path)))
    );

它可以直接使用path,因此您根本不必处理StreamReader

票数 3
EN

Stack Overflow用户

发布于 2013-10-18 20:02:44

尝试:

代码语言:javascript
复制
string lines = File.ReadAllText( path );

XDocument doc = new XDocument(
    new XComment( "You can copy and paste or open the XML in Excel." ),
    new XElement( "Root",
        new XElement( "logs", XElement.Parse( lines ) ) ) );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19458012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档