You must Sign In to post a response.
  • Category: ASP.NET

    Using C#, How to Save,Update Data to and from XML with out using the sql server database?

    Using C#, How to Save,Update Data, to and from XML with out using the sql server database?
  • #259819
    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>

    display all the data using a C# console application (see Listing 2).

    Listing 2: DisplayCatalog.cs

    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();

    Listing 2 is just an extract from the DisplayCatalog() method of the C# application. It displays the data from the XML file. It uses the XMLNodeList class to retrieve the relevant XML node and then iterates it with the help of the for loop and the Count property of the class. Inside the loop, it creates an instance of the XMLAttributeCollection class and displays the appropriate values using the properties of the class.

    Inside the constructor, the code creates an instance of the FileStream class and sets the required permissions (see Listing 3). It then loads the XML document with the help of the XMLDocument class and loads the required instance of the FileStream class with the Load() method of the XMLDocument class.

    Listing 3: DisplayCatalog.cs

    FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read,
    FileShare.ReadWrite);
    xmldoc = new XmlDocument();
    xmldoc.Load(fs);
    DisplayCatalog();

    Listing 4 appends a new catalog entry to the XML document using the various properties and methods of the XMLDocument class.

    Listing 4: AddCatalog.cs

    // New XML Element Created
    XmlElement newcatalogentry = xmldoc.CreateElement("Book");

    // New Attribute Created
    XmlAttribute newcatalogattr = xmldoc.CreateAttribute("ID");

    // Value given for the new attribute
    newcatalogattr.Value = "005";

    // Attach the attribute to the XML element
    newcatalogentry.SetAttributeNode(newcatalogattr);

    // First Element - Book - Created
    XmlElement firstelement = xmldoc.CreateElement("Book");

    // Value given for the first element
    firstelement.InnerText = "Peter";

    // Append the newly created element as a child element
    newcatalogentry.AppendChild(firstelement);


    // Second Element - Publisher - Created
    XmlElement secondelement = xmldoc.CreateElement("Publisher");

    // Value given for the second element
    secondelement.InnerText = "Que Publishing";

    // Append the newly created element as a child element
    newcatalogentry.AppendChild(secondelement);

    // New XML element inserted into the document
    xmldoc.DocumentElement.InsertAfter(newcatalogentry,
    xmldoc.DocumentElement.LastChild);

    // An instance of FileStream class created
    // The first parameter is the path to the XML file - Catalog.xml

    FileStream fsxml = new FileStream(path,FileMode.Truncate,
    FileAccess.Write,
    FileShare.ReadWrite);

    // XML Document Saved
    xmldoc.Save(fsxml);

    http://www.developer.com/net/csharp/article.php/3489611

  • #259971
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("C:\\Myxml\\Items.xml");
    XmlElement itemMain = xmlDoc.CreateElement("Item");
    XmlElement ItemNo = xmlDoc.CreateElement("ItemNo");
    ItemNo.InnerXml = TextBox1.Text;
    itemMain.AppendChild(ItemNo);

    XmlElement ItemName = xmlDoc.CreateElement("ItemName");
    ItemName.InnerXml = TextBox2.Text;
    itemMain.AppendChild(ItemName);

    XmlElement ItemCost = xmlDoc.CreateElement("ItemCost");
    ItemCost.InnerXml = TextBox3.Text;
    itemMain.AppendChild(ItemCost);


    xmlDoc.DocumentElement.AppendChild(itemMain);

    xtw = new XmlTextWriter("C:\\Myxml\\Items.xml",null);

    xmlDoc.WriteContentTo(xtw);
    xtw.Close();
    ClientScript.RegisterStartupScript(GetType(), "Hello", "<script>alert('Data has Appended')</script>");

  • #260104
    I'm writing Xml file in asp.net now my problem is that if i delete newly created xml file then this code is working actually i'm using stream writer still it is showing file exists it means that previous file is not appended or detroyed so plz tell me the soln
    This code is Asp.net & C#
    This is my code Plz check and correct it
    send me it soon
    stw = new StreamWriter("C:\\Myxml\\Items.xml",true);
    xtw = new XmlTextWriter(stw); //(stream, System.Text.Encoding.ASCII); //(stw);
    xtw.WriteStartDocument();
    xtw.WriteStartElement("ItemData");
    xtw.WriteStartElement("ItemsData");
    xtw.WriteStartElement("Item");
    xtw.WriteElementString("ItemNo", TextBox1.Text);
    xtw.WriteElementString("ItemName", TextBox2.Text);
    xtw.WriteElementString("ItemCost", TextBox3.Text);
    xtw.WriteEndElement();
    xtw.WriteEndElement();
    xtw.WriteEndDocument();
    xtw.Flush();
    ClientScript.RegisterStartupScript(GetType(), "Hello", "<script>alert('Data Written to Xml file Successfully...')</script>");
    TextBox1.Text = string.Empty;
    TextBox2.Text = string.Empty;
    TextBox3.Text = string.Empty;
    xtw.Close();


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.