How to add Xml node in existing xml file using c# [Resolved]

Posted by Karthik2010_Mca under C# on 6/28/2016 | Points: 10 | Views : 617 | Status : [Member] | Replies : 6
Hi

I want to add new xml node in existing xml file using c#.

Ex: My existing xml code like below,
<manifest xmlns:imsmd="http://ltsc.ieee.org/xsd/LOM">
<resources>
<resource>
<file href="default.xml">
<metadata>
<imsmd:lom />
</metadata>
</file>
</resource>
</resources>
-----------------------
In above existing xml file I want to add new node like below using c# code,
<manifest xmlns:imsmd="http://ltsc.ieee.org/xsd/LOM">
<resources>
<resource>
<file href="default.xml">
<metadata>
<imsmd:lom />
</metadata>
</file>
<file href="testimg.png">
<metadata>
<imsmd:lom />
</metadata>
</file>
</resource>
</resources>



Regards
Karthik

Karthik


Responses

Posted by: Rajnilari2015 on: 6/28/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Kartik Sir, try this
using System;

using System.Linq;
using System.Xml.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input = @"<manifest xmlns:imsmd=""http://ltsc.ieee.org/xsd/LOM""><resources><resource><file href=""default.xml""><metadata><imsmd:lom /></metadata></file></resource></resources></manifest>";
var doc = XDocument.Parse(input);
doc.Descendants("resource")
.ElementAt(0)
.Add(new XElement("file", new XAttribute("href", "testimg.png"),
new XElement("metadata", new XElement(doc.Root.GetNamespaceOfPrefix("imsmd") + "lom"))));

var str = doc.ToString();

Console.ReadKey();
}
}
}

The output of the program is
<manifest xmlns:imsmd="http://ltsc.ieee.org/xsd/LOM">

<resources>
<resource>
<file href="default.xml">
<metadata>
<imsmd:lom />
</metadata>
</file>
<file href="testimg.png">
<metadata>
<imsmd:lom />
</metadata>
</file>
</resource>
</resources>
</manifest>

Hope that helps

--
Thanks & Regards,
RNA Team

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 6/28/2016 [Member] [MVP] Bronze | Points: 25

Up
0
Down
You can use XDocument to read the xml file and nodes like given below

//Change the path name here

string filename = @"F:\AspNetWebApplication\EmptyWebApp\Test.xml";
//create new instance of XmlDocument
XmlDocument doc = new XmlDocument();

string nodeNamespace = "http://ltsc.ieee.org/xsd/LOM";
//load from file
doc.Load(filename);

//select your specific node ..
XmlElement el = (XmlElement)doc.SelectSingleNode("/resources/resource");

//create node and add value
XmlElement node = doc.CreateElement("file",null);
node.SetAttribute("href", "testimg.png");

//create metadata node
XmlElement metadataTitle = doc.CreateElement("metadata");

//create imsdm node
XmlElement imsdmnode = doc.CreateElement("imsmd","lom", nodeNamespace);

//add to parent node
metadataTitle.AppendChild(imsdmnode);
node.AppendChild(metadataTitle);

//add to elements collection
doc.DocumentElement.AppendChild(node);

//save back
doc.Save(filename);


Thanks,
A2H
Please mark my reply as answer if it helps to resolve your problem

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 6/28/2016 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Reference URL : https://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v=vs.110).aspx

Thanks,
A2H
Please mark my reply as answer if it helps to resolve your problem

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthik2010_Mca on: 6/28/2016 [Member] Starter | Points: 25

Up
0
Down
Hi

Above code is working fine. but new node has been added before </resource> node. New node should be add after </file> node.

Even I was trying this code
XmlElement el = (XmlElement)doc.SelectSingleNode("/resources");

That also it is updating after </resource> node.


Thanks
Karthik


Karthik

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 6/28/2016 [Member] [MVP] Bronze | Points: 25

Up
0
Down
You wrote "but new node has been added before </resource> node. New node should be add after </file> node. "

To resolve that issue you can try with the below code
//Change the path name here
string filename = @"F:\AspNetWebApplication\EmptyWebApp\Test.xml";
//create new instance of XmlDocument
XmlDocument doc = new XmlDocument();
string nodeNamespace = "http://ltsc.ieee.org/xsd/LOM";
//load from file
doc.Load(filename);

//select your specific node ..
XmlElement file = (XmlElement)doc.SelectSingleNode("/manifest/resources/resource");

//create node and add value
XmlElement node = doc.CreateElement("file",null);
node.SetAttribute("href", "testimg.png");

//create metadata node
XmlElement metadataTitle = doc.CreateElement("metadata");

//create imsdm node
XmlElement imsdmnode = doc.CreateElement("imsmd","lom", nodeNamespace);

//add to parent node
metadataTitle.AppendChild(imsdmnode);
node.AppendChild(metadataTitle);

//add to elements collection
file.AppendChild(node);

//save back
doc.Save(filename);


Thanks,
A2H
Please mark my reply as answer if it helps to resolve your problem

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthik2010_Mca on: 6/29/2016 [Member] Starter | Points: 25

Up
0
Down

Hi

After updating the above code, then also the new node is came after </resources> node.


Thanks

Karthik

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response