Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
I am writing a small application, it has 4 text boxes and a button. On press of the button, i would like to be able to write the 4 values to an XML file. Tomorrow i would like to do it again, and again the next day. I know the structure of an XML file. I can write them in my sleep (well, basic ones anyway). I would like to have my application write to an XML file. Any real good examples floating around? I prefer any C# examples if possible. Thank you in advance for any and all help on this issue.
..::[ kleptos ]::..
Posted

Here is an example I found on how to read a XML File

 

namespace HowTo.Samples.XML
{

using System;
using System.IO;
using System.Xml;

public class ReadXmlFileSample
{
   private const string document = "books.xml";

   public static void Main()
   {
       ReadXmlFileSample myReadXmlFileSample = new ReadXmlFileSample();
       myReadXmlFileSample.Run(document);
   }

   public void Run(String args)
   {
       XmlTextReader reader = null;

       try
       {
           // Load the file with an XmlTextReader
           Console.WriteLine ("Reading file {0} ...", args);
           reader = new XmlTextReader (args);
           Console.WriteLine ("File {0} read sucessfully ...", args);

           // Process the supplied XML file
           Console.WriteLine ("Processing ...");
           Console.WriteLine ();
           FormatXml(reader, args);
       }
       catch (Exception e)
       {
           Console.WriteLine ("Failed to read the file {0}", args);
           Console.WriteLine ("Exception: {0}", e.ToString());
       }

       finally
       {
           Console.WriteLine();
           Console.WriteLine("Processing of the file {0} complete.", args);
           // Finished with XmlTextReader
           if (reader != null)
               reader.Close();
       }
   }

   private static void FormatXml (XmlReader reader, String filename)
   {
       int declarationCount=0, piCount=0, docCount=0, commentCount=0, elementCount=0, attributeCount=0, textCount=0, whitespaceCount=0;

       while (reader.Read())
       {
           switch (reader.NodeType)
           {
           case XmlNodeType.XmlDeclaration:
               Format (reader, "XmlDeclaration");
               declarationCount++;
               break;
           case XmlNodeType.ProcessingInstruction:
               Format (reader, "ProcessingInstruction");
               piCount++;
               break;
           case XmlNodeType.DocumentType:
               Format (reader, "DocumentType");
               docCount++;
               break;
           case XmlNodeType.Comment:
               Format (reader, "Comment");
               commentCount++;
               break;
           case XmlNodeType.Element:
               Format (reader, "Element");
               elementCount++;
               if (reader.HasAttributes)
                   attributeCount += reader.AttributeCount;
               break;
           case XmlNodeType.Text:
               Format (reader, "Text");
               textCount++;
               break;
           case XmlNodeType.Whitespace:
               whitespaceCount++;
               break;
           }
       }

       // Display the Statistics for the file.
       Console.WriteLine ();
       Console.WriteLine("Statistics for {0} file", filename);
       Console.WriteLine ();
       Console.WriteLine("XmlDeclaration: {0}",declarationCount++);
       Console.WriteLine("ProcessingInstruction: {0}",piCount++);
       Console.WriteLine("DocumentType: {0}",docCount++);
       Console.WriteLine("Comment: {0}",commentCount++);
       Console.WriteLine("Element: {0}",elementCount++);
       Console.WriteLine("Attribute: {0}",attributeCount++);
       Console.WriteLine("Text: {0}",textCount++);
       Console.WriteLine("Whitespace: {0}",whitespaceCount++);
   }

   private static void Format(XmlReader reader, String nodeType)
   {
       // Format the output
       Console.Write(reader.Depth + " ");
       Console.Write(reader.AttributeCount + " ");
       for (int i=0; i < reader.Depth; i++)
       {
           Console.Write('\t');
       }

       Console.Write(reader.Prefix + nodeType + "<" + reader.Name + ">" + reader.Value);

       // Display the attributes values for the current node
       if (reader.HasAttributes)
       {
           Console.Write(" Attributes:");

           for (int j=0; j < reader.AttributeCount; j++)
           {
               Console.Write(" [{0}] " + reader[j], j);
           }
       }
       Console.WriteLine();
   }

} // End class ReadXmlFileSample
} // End namespace HowTo.Samples.XML

Fat kids are harder to kidnap
Posted

And this example on how to write a XML

 

namespace HowTo.Samples.XML
{

using System;
using System.IO;
using System.Xml;

public class WriteXmlFileSample
{
   private const string document = "newbooks.xml";

   public static void Main()
   {
       WriteXmlFileSample myWriteXmlFileSample = new WriteXmlFileSample();
       myWriteXmlFileSample.Run(document);
   }

   public void Run(String args)
   {
       XmlTextWriter myXmlTextWriter = null;
       XmlTextReader myXmlTextReader = null;

       try
       {
           myXmlTextWriter = new XmlTextWriter (args, null);

           myXmlTextWriter.Formatting = Formatting.Indented;
           myXmlTextWriter.WriteStartDocument(false);
           myXmlTextWriter.WriteDocType("bookstore", null, "books.dtd", null);
           myXmlTextWriter.WriteComment("This file represents another fragment of a book store inventory database");
           myXmlTextWriter.WriteStartElement("bookstore");
           myXmlTextWriter.WriteStartElement("book", null);
           myXmlTextWriter.WriteAttributeString("genre","autobiography");
           myXmlTextWriter.WriteAttributeString("publicationdate","1979");
           myXmlTextWriter.WriteAttributeString("ISBN","0-7356-0562-9");
           myXmlTextWriter.WriteElementString("title", null, "The Autobiography of Mark Twain");
           myXmlTextWriter.WriteStartElement("Author", null);
           myXmlTextWriter.WriteElementString("first-name", "Mark");
           myXmlTextWriter.WriteElementString("last-name", "Twain");
           myXmlTextWriter.WriteEndElement();
           myXmlTextWriter.WriteElementString("price", "7.99");
           myXmlTextWriter.WriteEndElement();
           myXmlTextWriter.WriteEndElement();

           //Write the XML to file and close the writer
           myXmlTextWriter.Flush();
           myXmlTextWriter.Close();

           // Read the file back in and parse to ensure well formed XML
           myXmlTextReader = new XmlTextReader (args);
           FormatXml (myXmlTextReader, args);
       }
       catch (Exception e)
       {
           Console.WriteLine ("Exception: {0}", e.ToString());
       }

       finally
       {
           Console.WriteLine();
           Console.WriteLine("Processing of the file {0} complete.", args);
           if (myXmlTextReader != null)
               myXmlTextReader.Close();
           //Close the writer
           if (myXmlTextWriter != null)
               myXmlTextWriter.Close();
       }
   }

   private static void FormatXml (XmlTextReader reader, String filename)
   {
       int piCount=0, docCount=0, commentCount=0, elementCount=0, attributeCount=0, textCount=0, whitespaceCount=0;

       while (reader.Read())
       {
           switch (reader.NodeType)
           {
           case XmlNodeType.ProcessingInstruction:
               Format (reader, "ProcessingInstruction");
               piCount++;
               break;
           case XmlNodeType.DocumentType:
               Format (reader, "DocumentType");
               docCount++;
               break;
           case XmlNodeType.Comment:
               Format (reader, "Comment");
               commentCount++;
               break;
           case XmlNodeType.Element:
               Format (reader, "Element");
               while(reader.MoveToNextAttribute())
               {
                   Format (reader, "Attribute");
               }
               elementCount++;
               
               if (reader.HasAttributes)
                   attributeCount += reader.AttributeCount;
               break;
           case XmlNodeType.Text:
               Format (reader, "Text");
               textCount++;
               break;
           case XmlNodeType.Whitespace:
               whitespaceCount++;
               break;
           }
       }

       // Display the Statistics for the file
       Console.WriteLine ();
       Console.WriteLine("Statistics for {0} file", filename);
       Console.WriteLine ();
       Console.WriteLine("ProcessingInstruction: {0}",piCount++);
       Console.WriteLine("DocumentType: {0}",docCount++);
       Console.WriteLine("Comment: {0}",commentCount++);
       Console.WriteLine("Element: {0}",elementCount++);
       Console.WriteLine("Attribute: {0}",attributeCount++);
       Console.WriteLine("Text: {0}",textCount++);
       Console.WriteLine("Whitespace: {0}",whitespaceCount++);
   }

   // Format the output
   private static void Format(XmlReader reader, String NodeType)
   {
       // Format the output
       Console.Write(reader.Depth + " ");
       Console.Write(reader.AttributeCount + " ");

       for (int i=0; i < reader.Depth; i++)
       {
           Console.Write('\t');
       }

       Console.Write(reader.Prefix + NodeType + "<" + reader.Name + ">" + reader.Value);
       Console.WriteLine();
   }

} // End class WriteXmlFileSample
} // End namespace HowTo.Samples.XML

Fat kids are harder to kidnap

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...