Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to add root node to an XML File

Posted on 2010-09-02
3
Medium Priority
?
955 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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 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