Solved

How to add root node to an XML File

Posted on 2010-09-02
3
936 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# XML Get Values 4 52
Return array 3 34
Inserting LocalDB Table to SQL Server C# 3 48
How to get the range for the percentage from user input. 4 25
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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 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