Solved

How to add root node to an XML File

Posted on 2010-09-02
3
902 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
Comment Utility
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
Comment Utility
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
Comment Utility
lala
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Object Oriented Best Practice 5 32
LINQ question 7 20
c# code 19 56
dynamic menu in asp.net c# 11 26
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now