Omid Gerami
Omid Gerami

Reputation: 9

Inserting Some lines to an xml file in c#

Hi I need to insert some lines into an xml file and save it how I should do it?

the xml file is

<?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>

and I need to insert these lines

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

between

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

Upvotes: 0

Views: 218

Answers (3)

user1064248
user1064248

Reputation:

A quick approach would be:

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

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

Here is a complete console application that will take the file you provided as input and build a new file with the nodes added.

NOTE: you will clearly have to make the code more dynamic as this is very static. For example, you would build the Column elements with another list and a loop likely.

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");
    }
}

The output of the new file looks like this:

  <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>

Upvotes: 2

Jeet Bhatt
Jeet Bhatt

Reputation: 778

Try this,

 XmlDocument document = new XmlDocument();
document.Load(filename);
XmlElement childElement = document.CreateElement("child");
XmlNode parentNode = document.SelectSingleNode("root/firstLevel/parent");
parentNode.AppendChild(childElement);

Upvotes: 0

Related Questions