Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# create simple xml file [closed]

Tags:

c#

xml

How can I create a simple xml file and store it in my system?

like image 998
Muhammad Zeeshan Avatar asked Nov 04 '10 08:11

Muhammad Zeeshan


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

You could use XDocument:

new XDocument(     new XElement("root",          new XElement("someNode", "someValue")         ) ) .Save("foo.xml"); 

If the file you want to create is very big and cannot fit into memory you might use XmlWriter.

like image 149
Darin Dimitrov Avatar answered Oct 10 '22 02:10

Darin Dimitrov


I'd recommend serialization,

public class Person {       public  string FirstName;       public  string MI;       public  string LastName; }  static void Serialize() {       clsPerson p = new Person();       p.FirstName = "Jeff";       p.MI = "A";       p.LastName = "Price";       System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());       x.Serialize(System.Console.Out, p);       System.Console.WriteLine();       System.Console.WriteLine(" --- Press any key to continue --- ");       System.Console.ReadKey(); } 

You can further control serialization with attributes.
But if it is simple, you could use XmlDocument:

using System; using System.Xml;  public class GenerateXml {     private static void Main() {         XmlDocument doc = new XmlDocument();         XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);         doc.AppendChild(docNode);          XmlNode productsNode = doc.CreateElement("products");         doc.AppendChild(productsNode);          XmlNode productNode = doc.CreateElement("product");         XmlAttribute productAttribute = doc.CreateAttribute("id");         productAttribute.Value = "01";         productNode.Attributes.Append(productAttribute);         productsNode.AppendChild(productNode);          XmlNode nameNode = doc.CreateElement("Name");         nameNode.AppendChild(doc.CreateTextNode("Java"));         productNode.AppendChild(nameNode);         XmlNode priceNode = doc.CreateElement("Price");         priceNode.AppendChild(doc.CreateTextNode("Free"));         productNode.AppendChild(priceNode);          // Create and add another product node.         productNode = doc.CreateElement("product");         productAttribute = doc.CreateAttribute("id");         productAttribute.Value = "02";         productNode.Attributes.Append(productAttribute);         productsNode.AppendChild(productNode);         nameNode = doc.CreateElement("Name");         nameNode.AppendChild(doc.CreateTextNode("C#"));         productNode.AppendChild(nameNode);         priceNode = doc.CreateElement("Price");         priceNode.AppendChild(doc.CreateTextNode("Free"));         productNode.AppendChild(priceNode);          doc.Save(Console.Out);     } } 

And if it needs to be fast, use XmlWriter:

public static void WriteXML() {     // Create an XmlWriterSettings object with the correct options.     System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();     settings.Indent = true;     settings.IndentChars = "    "; //  "\t";     settings.OmitXmlDeclaration = false;     settings.Encoding = System.Text.Encoding.UTF8;      using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create("data.xml", settings))     {          writer.WriteStartDocument();         writer.WriteStartElement("books");          for (int i = 0; i < 100; ++i)         {             writer.WriteStartElement("book");             writer.WriteElementString("item", "Book "+ (i+1).ToString());             writer.WriteEndElement();         }          writer.WriteEndElement();          writer.Flush();         writer.Close();     } // End Using writer   } 

And btw, the fastest way to read XML is XmlReader:

public static void ReadXML() {     using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"))     {         while (xmlReader.Read())         {             if ((xmlReader.NodeType == System.Xml.XmlNodeType.Element) && (xmlReader.Name == "Cube"))             {                 if (xmlReader.HasAttributes)                     System.Console.WriteLine(xmlReader.GetAttribute("currency") + ": " + xmlReader.GetAttribute("rate"));             }          } // Whend       } // End Using xmlReader      System.Console.ReadKey(); } 

And the most convenient way to read XML is to just deserialize the XML into a class.
This also works for creating the serialization classes, btw.
You can generate the class from XML with Xml2CSharp:
https://xmltocsharp.azurewebsites.net/

like image 41
Stefan Steiger Avatar answered Oct 10 '22 03:10

Stefan Steiger