?
Solved

How to add root node to an XML File

Posted on 2010-09-02
3
Medium Priority
?
945 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 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