Manipulate XML File Data Using C#
Nearly 95 percent of .NET applications use XML for various tasks. A main usage for Web developers is combining XML with HTML to display information on Web pages, which relieves them from having to spend a long time editing the content on their Web pages. A single change to an XML file will be reflected across the entire Web site, thus simplifying the development and also greatly reducing the development time.
Display Contents of XML File
Listing 1 shows a simple XML file for demonstration:
Listing 1
<?xml version="1.0"?>
<Books>
<Book ID="001">
<Author>Mark</Author>
<Publisher>Sams</Publisher>
</Book>
<Book ID="002">
<Author>Joe</Author>
<Publisher>AWL</Publisher>
</Book>
</Books>
The next step is to display all the data using a C# console application (see Listing 2).
XmlNodeList xmlnode = xmldoc.GetElementsByTagName("Book"); Console.WriteLine("Here is the list of catalogs\n\n");for(int i=0;i<xmlnode.Count;i++)
{XmlAttributeCollection xmlattrc = xmlnode[i].Attributes;
//XML Attribute Name and Value returned //Example: <Book id = "001">Console.Write(xmlattrc[0].Name);
Console.WriteLine(":\t"+xmlattrc[0].Value); //First Child of the XML file - Catalog.xml - returned //Example: <Author>Mark</Author>Console.Write(xmlnode[i].FirstChild.Name);
Console.WriteLine(":\t"+xmlnode[i].FirstChild.InnerText); //Last Child of the XML file - Catalog.xml - returned //Example: <Publisher>Sams</Publisher>Console.Write(xmlnode[i].LastChild.Name);
Console.WriteLine(":\t"+xmlnode[i].LastChild.InnerText);Console.WriteLine();
FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read,
FileShare.ReadWrite);
xmldoc = new XmlDocument();
xmldoc.Load(fs);
DisplayCatalog();
You can download the complete source code for all the from the code download section at the end of this article.
The final output looks like the display in Figure 1.
Figure 1: Final Output from Listings 2 and 3
Write Data Directly to XML File
// New XML Element CreatedXmlElement newcatalogentry = xmldoc.CreateElement("Book");// New Attribute CreatedXmlAttribute newcatalogattr = xmldoc.CreateAttribute("ID");// Value given for the new attributenewcatalogattr.Value = "005";// Attach the attribute to the XML elementnewcatalogentry.SetAttributeNode(newcatalogattr);
// First Element - Book - CreatedXmlElement firstelement = xmldoc.CreateElement("Book");// Value given for the first elementfirstelement.InnerText = "Peter";// Append the newly created element as a child elementnewcatalogentry.AppendChild(firstelement);
// Second Element - Publisher - CreatedXmlElement secondelement = xmldoc.CreateElement("Publisher");// Value given for the second elementsecondelement.InnerText = "Que Publishing";// Append the newly created element as a child elementnewcatalogentry.AppendChild(secondelement);
// New XML element inserted into the documentxmldoc.DocumentElement.InsertAfter(newcatalogentry,
xmldoc.DocumentElement.LastChild);
// An instance of FileStream class created// The first parameter is the path to the XML file - Catalog.xmlFileStream fsxml = new FileStream(path,FileMode.Truncate,FileAccess.Write,
FileShare.ReadWrite);
// XML Document Savedxmldoc.Save(fsxml);
No comments:
Post a Comment