Solved

How to add root node to an XML File

Posted on 2010-09-02
3
921 Views
Last Modified: 2012-05-10
HI ;

I have the following XML File

  <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <SSISPackageLayout>
           <DateCreated>2010-08-05T16:10:27.567-04:00</DateCreated>
           <DateEff>2010-08-05T16:10:27.567-04:00</DateEff>
  </SSISPackageLayout>

I want to add an root node like that

  <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

- <SSISPackageInfoDesc>
     - <SSISPackageLayout>
             <DateCreated>2010-08-05T16:10:27.567-04:00</DateCreated>
             <DateEff>2010-08-05T16:10:27.567-04:00</DateEff>
     </SSISPackageLayout>
- <SSSISPackageInfoDesc>

My question is how to add the   <SSISPackageInfoDesc> node to my existing xml using C#

Thank's
0
Comment
Question by:Fatehbourghoud
3 Comments
 
LVL 12

Expert Comment

by:w00te
ID: 33586758
Well, I'd consider just opening the file as text in C# and tabbing every line after adding what you want to the start/end (no special XML handling is needed for that operation).  It's a sloppy workaround, but it would be easy and quick to implement.
I'm not aware of a way to add a root node to an existing file that doesn't have one though using the XML classes (that doesn't mean one doesn't exist though, haha).
Good luck, and I hope you get a better answer to this one! :p
-w00te
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 33609209
Let's start with this: there is not an XML editing functionality in .net. Although can sound easy for your case, It would be very complex to manage in most cases.
What you basically do is to write a new file with the whole updated xml. There are 2 main ways to do it:
1) Load the entire xml into memory, add the desired node and write the whole document by overwritting the original file. This works well if xml file is small.
2) Load the xml contents, record by record, and write it into a new file, write the new node when pertinent and close the file.

Both ways are supported by .net. In the first case you can use an XmlDocument and in the second one you can use an XmlReader and XmlWriter. I suggest the first one.

Something like the attached code snippet.
XmlDocument oldDoc = new XmlDocument();
xamlDoc.Load("YourXmlFile.xml");

XmlDocument newDoc = new XmlDocument();
XmlElement root = newDoc.CreateElement("SSISPackageLayout");
newDoc.InsertAfter(root, null);

XmlNode content = newDoc.ImportNode(oldDoc.FirstChild, true);
root.AppendChild(content);

newDoc.Save("YourXmlFile.xml");

Open in new window

0
 

Author Closing Comment

by:Fatehbourghoud
ID: 33675801
lala
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question