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.
Answers were Sorted based on User's Feedback
Answer / bala
r u modifying the XML file by progm....
if u r modifying the xml by prgm means u have to specify
that excat XML path where it is located..
otherwise u can use static property
then u ll get it
| Is This Answer Correct ? | 7 Yes | 1 No |
Answer / 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 |
Answer / tajas
hello first of all i am sorry that i posted it as answer.But
this is the exact same problem that i am having so i am here
to ask a question.I understood the code but the problem is
that i dont happen to have xml file in a physical location.I
have to read xml from website directly.How do i do that?Any
help is very much appreciated.
| Is This Answer Correct ? | 0 Yes | 3 No |
What is ildasm.exe used for?
In Main function another function is there and to that function if we pass string as parameter ,then that string value is passed by value or reference type?
What is a decimal c#?
What is a method signature c#?
What namespaces are necessary to create a localized application?
what are the contents of an assembly ?
Explain how to parse a datetime string?
give suitable code for all login controls
Trace the O/p for a program on inheritance and Polymorphism. 3 classes A,B,C A with two methods public virtual SomeMethodA public virtual SomemoreMethodA B:A overide virtual SomeMethodA C:B new Method SomeMethodA override SomeMoreMethodA main method { b new instance of B b.SomeMethodA b.SomeMoreMethodA b1 new instance of C b1.SomeMethodA b1.SomeMoreMethodA }
In howmany ways can you deploy an assembly?
What namespaces are necessary to create a localized application?
class Employee { stat9c Method1() static Method2(ref a) stat9c Method3(out x) } class Employee { stat9c Method1() static Method2(ref a) stat9c Method3(out x) } what will happen & why?
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)