0

嗨,我需要在 xml 文件中插入一些行并保存它我应该怎么做?

xml文件是

<?xml version="1.0" encoding="utf-8"?>
<Dashboard CurrencyCulture="en-US">
  <Title Text="Dashboard" />
  <DataConnections>
    <DataConnection Name="Database1Connection" ProviderKey="Access2007" ConnectionString="XpoProvider=MSAccess;Provider=Microsoft.ACE.OLEDB.12.0;Mode=Share Deny None;data source=D:\Sina\Desktop\Omid\Database1.accdb;Jet OLEDB:Database Password=;">
      <Parameters>
        <Parameter Name="database" Value="D:\Sina\Desktop\Omid\Database1.accdb" />
        <Parameter Name="read only" Value="1" />
        <Parameter Name="generateConnectionHelper" Value="false" />
      </Parameters>
    </DataConnection>
  </DataConnections>
  <DataSources>
    <DataSource Name="Data Source 1">
      <DataProvider DataConnection="Database1Connection" SupportSql="true" />
    </DataSource>
    <DataSource Name="Query 2" />
  </DataSources>

我需要插入这些行

    <Selection>
      <Table Name="Query2">
        <Columns>
          <Column Name="PName" />
          <Column Name="Prog" />
          <Column Name="RDate" />
        </Columns>
      </Table>
    </Selection>

之间

      <DataProvider DataConnection="Database1Connection" SupportSql="true">
.
.(here)
      </DataProvider> 
4

3 回答 3

2

这是一个完整的控制台应用程序,它将您提供的文件作为输入并构建一个添加了节点的新文件。

注意:您显然必须使代码更加动态,因为这是非常静态的。例如,您可能会Column使用另一个列表和一个循环来构建元素。

class Program
{
    static void Main(string[] args)
    {
        var doc = XDocument.Load("XMLFile1.xml");

        var selection = new XElement("Selection");

        var table = new XElement("Table");
        table.Add(new XAttribute("Name", "Query2"));

        var columns = new XElement("Columns");

        var column = new XElement("Column");
        column.Add(new XAttribute("Name", "PName"));
        columns.Add(column);

        column = new XElement("Column");
        column.Add(new XAttribute("Name", "Prog"));
        columns.Add(column);

        column = new XElement("Column");
        column.Add(new XAttribute("Name", "RDate"));
        columns.Add(column);

        table.Add(columns);

        selection.Add(table);

        var dataProvider = doc.Root.Descendants("DataProvider").First();
        dataProvider.Add(selection);

        doc.Save("XMLFile2.xml");
    }
}

新文件的输出如下所示:

  <DataSources>
    <DataSource Name="Data Source 1">
      <DataProvider DataConnection="Database1Connection" SupportSql="true">
        <Selection>
          <Table Name="Query2">
            <Columns>
              <Column Name="PName" />
              <Column Name="Prog" />
              <Column Name="RDate" />
            </Columns>
          </Table>
        </Selection>
      </DataProvider>
    </DataSource>
    <DataSource Name="Query 2" />
  </DataSources>
于 2013-10-14T12:59:50.520 回答
0

尝试这个,

 XmlDocument document = new XmlDocument();
document.Load(filename);
XmlElement childElement = document.CreateElement("child");
XmlNode parentNode = document.SelectSingleNode("root/firstLevel/parent");
parentNode.AppendChild(childElement);
于 2013-10-14T12:59:39.580 回答
0

一种快速的方法是:

    string file = "XMLFile1.xml";
    string text = File.ReadAllText(file);
    text = text.Replace("<DataProvider DataConnection=\"Database1Connection\" SupportSql=\"true\" />",
        "<DataProvider DataConnection=\"Database1Connection\" SupportSql=\"true\">" +
        "<Selection>" +
         "<Table Name=\"Query2\">" +
         "<Columns>" +
         " <Column Name=\"PName\" />" +
          "<Column Name=\"Prog\" />" +
          "<Column Name=\"RDate\" />" +
         "</Columns>" +
         "</Table>" +
         "</Selection>" +
        "</DataProvider> ");
    File.WriteAllText(file, text);
于 2013-10-14T13:12:01.023 回答