November 30, 2006

Reading and Writing Xml


#region Read/Write Xml

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

...

// Reading from XML Reader
private void FromXml(XmlReader reader)
{
   bool corrupt = false;
   while (!reader.EOF && !corrupt)
   {
       if (reader.NodeType != XmlNodeType.EndElement)
       {
           switch (reader.Name)
           {
               case "BaseElement":
                 string tmp = reader.GetAttribute("Guid");
                 m_Guid = new Guid(tmp);                    
               break;

               case "EnumType":
                  string tmp = reader.ReadElementContentAsString();
                  m_EnumType = (EnumType)Enum.Parse(
                       typeof(EnumType), tmp);
               break;
               case "Id":
                   m_Id = reader.ReadElementContentAsString();
               break;
               case "Name":
                   m_SomeThing = reader.ReadElementContentAsString();
               break;
               case "SomeDate":
                   string value = reader.ReadElementContentAsString();
                   DateTime m_SomeDate;
                   DateTime.TryParse(value, out m_SomeDate);
               break;
               default:
               break;
           }
       }
       try
       {
           reader.Read();
       }
       catch (XmlException)
       {
           corrupt = true;
       }
   }
}


// Reading from XML String
private void FromXmlString(string xml)
{
   XmlTextReader reader = new XmlTextReader(xml, XmlNodeType.Element,
       new XmlParserContext(null, new XmlNamespaceManager(
           new NameTable()), null, XmlSpace.Default));
   try
   {
       reader.MoveToContent();
       FromXml(reader);
   }
   finally
   {
       reader.Close();
   }
}

// Reading from XMLFile
public void FromXmlFile(string xmlFile)
{
   if (File.Exists(xmlFile))
   {
       XmlTextReader reader = new XmlTextReader(xmlFile);
       try
       {
           reader.MoveToContent();
           FromXml(reader);
       }
       finally
       {
           reader.Close();
       }
   }
}

// Writing to XML Writer
public void ToXml(XmlWriter writer)
{
   //m_Writer.WriteDocType("", null, null, null);
   writer.WriteStartElement("BaseElement");
   writer.WriteAttributeString("Guid", m_Guid.ToString());
   WriteContent(writer);
   writer.WriteEndElement();
   writer.Flush();
}

protected virtual void WriteContent(XmlWriter writer)
{
   writer.WriteElementString("EnumType", m_EnumType.ToString());
   writer.WriteElementString("Id", m_Id);
   writer.WriteElementString("Name", m_Name);
   writer.WriteElementString("Date", DateTime.Now.ToUniversalTime().ToString());
}

// Writing to XML String
public override string ToXmlString()
{
   StringWriter sw = new StringWriter();
   XmlTextWriter writer = new XmlTextWriter(sw);
   try
   {
       writer.Formatting = Formatting.Indented;
       writer.WriteStartDocument();
       ToXml(writer);
       writer.WriteEndDocument();
   }
   finally
   {
       writer.Close();
   }

   return sw.GetStringBuilder().ToString();
}


public void ToXmlFile(string xmlFile, Encoding encoding)
{
   XmlTextWriter writer = new XmlTextWriter(xmlFile, encoding);
   try
   {
       writer.Formatting = Formatting.Indented;
       writer.WriteStartDocument();
       ToXml(writer);
       writer.WriteEndDocument();
   }
   finally
   {
       writer.Close();
   }
}

#endregion Read/Write Xml

No comments: