2

我正在尝试获取现有的 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))
    );

我最终得到这样的事情:

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

需要一些解码编码帮助。

4

2 回答 2

3

改用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)))
    );

它可以path直接取走,所以你根本不用处理StreamReader

于 2013-10-18T20:02:27.360 回答
1

尝试:

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 ) ) ) );
于 2013-10-18T20:02:44.677 回答