Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用StreamReader文本中的XElement创建新的XDocument_C#_Asp.net_Xml_Linq To Xml - Fatal编程技术网

C# 使用StreamReader文本中的XElement创建新的XDocument

C# 使用StreamReader文本中的XElement创建新的XDocument,c#,asp.net,xml,linq-to-xml,C#,Asp.net,Xml,Linq To Xml,我试图获取一个现有的XML文件,并将其“嵌入”到另一个“根”XML文件的另一个节点中。假设存在指向现有xml文件的路径 StreamReader reader = new StreamReader(path); string lines = reader.ReadLine(); while (!reader.EndOfStream) { lines = reader.ReadLine(); } XDocument doc = new

我试图获取一个现有的XML文件,并将其“嵌入”到另一个“根”XML文件的另一个节点中。假设存在指向现有xml文件的路径

 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))
    );
我的结局是这样的:

logusernameonomy/

需要一些解码编码帮助。

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

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)))
    );
它可以直接使用
路径
,因此您根本不必处理
StreamReader

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

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)))
    );
它可以直接使用
路径
,因此您根本不必处理
StreamReader

试试:

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 ) ) ) );
尝试:


StreamReader旨在读取纯文本;如果文件包含XML,请将其视为XML,而不是文本。参见MarcinJuraszek的回答StreamReader旨在阅读纯文本;如果文件包含XML,请将其视为XML,而不是文本。看看MarcinJuraszek的回答我明白为什么人们会说“saweeet!”谢谢!我明白为什么人们会说“saweeet!”谢谢!