Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

I have to create a windows application using C# .net to
Modifiy the XML file. The application have to show the node
and node value. plz help me.

Answer Posted / ravikiran gullapalli

Try to understand this code so that u will get some idea
how to change the data in the existing xml file.

using System;
using System.Xml;
using System.Text;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
// Create an XML document instance, and load
XML data.
XmlDocument doc = new XmlDocument();
doc.Load
("Q317666.xml"); // This code assumes
that the XML file is in the same folder.

// I. Modification
// 1. Increment all of the Book Id attribute
values by 100.
XmlNodeList nodeList = doc.SelectNodes
("//Book");
foreach (XmlNode node in nodeList)
node.Attributes["Id"].Value = (Int32.Parse
(node.Attributes["Id"].Value) + 100).ToString();

// 2. Change the book titles to uppercase
letters.
foreach (XmlNode node in nodeList)
node.FirstChild.InnerText =
(node.FirstChild.InnerText).ToUpper();

// 3. Modify the XML declaration instruction to
have Unicode encoding.
XmlDeclaration decl = (XmlDeclaration)
doc.FirstChild;
decl.Encoding = "UTF-16";

// II. Addition
// 1. Create a new Book element.
XmlElement newElem = doc.CreateElement("Book");

// Add the Id attribute.
XmlAttribute newAttr = doc.CreateAttribute
("Id");
newAttr.Value = "103";
newElem.Attributes.Append(newAttr);

// Create the child nodes. This code
demonstrates various ways to add them.
newElem.InnerXml
= "<Title></Title><Author></Author>";
XmlText txtNode = doc.CreateTextNode("A BRIEF
HISTORY OF TIME");
newElem.FirstChild.AppendChild(txtNode);
newElem.AppendChild(doc.CreateWhitespace
("\r\n")); // Linefeed
newElem["Author"].InnerText = "Stephen Hawking";

// 2. Add the new element to the end of the
book list.
doc.DocumentElement.AppendChild(newElem);

// III. Deletion
// 1. Remove the Genre nodes from Book elements.
foreach (XmlNode node in nodeList)
node.RemoveChild(node.SelectSingleNode
("Genre"));

// Display the output in Debug window.
System.Diagnostics.Debug.Write("{0}\n",
doc.OuterXml);

// 2. Save the modified XML to a file in
Unicode format.
doc.PreserveWhitespace = true;
XmlTextWriter wrtr = new XmlTextWriter
("Q317666_Out.xml", Encoding.Unicode);
doc.WriteTo(wrtr);
wrtr.Close();
}
catch(XmlException xmlEx) // Handle the Xml
exceptions here.
{
Console.WriteLine("{0}", xmlEx.Message);
}
catch(Exception ex) // Handle the
generic exceptions. here
{
Console.WriteLine("{0}", ex.Message);
}
}
}
}

Is This Answer Correct ?    7 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can multiple inheritance implemented in c# ?

866


How can we set the class to be inherited, but prevent the method from being over-ridden?

917


Write a C# program to find the Factorial of n

925


How much time will it take to learn unity?

868


What is verbatim literal in c#?

836


Can abstract classes be final?

891


What is lazy t?

898


How long will it take to learn c sharp?

881


What is the property of class?

992


What is the difference between class and namespace?

914


What is the difference between asp net and c#?

871


What is _viewstart cshtml?

832


What operators can be used to cast from one reference type to another without the risk of throwing an exception?

847


What is data type c#?

880


Explain partial class in c#?

856