Read & Write XML File using XMLDocument
Write XML File Using Xml Writer,
- public void WriteXMLFileUsingXmlWriter()
- {
- List < LumiaMobile > LumiaMobiles = new List < LumiaMobile > ();
- LumiaMobiles.Add(new LumiaMobile
- {
- ModelId = "lumia550",
- Height = "136.1 mm",
- Width = "67.8 mm",
- Thickness = "9.9 mm",
- Weight = "141.9 g"
- });
- LumiaMobiles.Add(new LumiaMobile
- {
- ModelId = "lumia950",
- Height = "145 mm",
- Width = "73.2 mm",
- Thickness = "8.2 mm",
- Weight = "150 g"
- });
- LumiaMobiles.Add(new LumiaMobile
- {
- ModelId = "Text",
- Height = "",
- Width = "",
- Thickness = "8.2 mm",
- Weight = "150 g"
- });
- using(XmlWriter writer = XmlWriter.Create("LumiaMobiles.xml"))
- {
- writer.WriteStartDocument();
- writer.WriteStartElement("LumiaMobiles");
- foreach(LumiaMobile lumiaMobile in LumiaMobiles)
- {
- writer.WriteStartElement(lumiaMobile.ModelId);
- writer.WriteElementString("Height", lumiaMobile.Height);
- writer.WriteElementString("Width", lumiaMobile.Width);
- writer.WriteElementString("Thickness", lumiaMobile.Thickness);
- writer.WriteElementString("Weight", lumiaMobile.Weight);
- writer.WriteEndElement();
- }
- writer.WriteEndElement();
- writer.WriteEndDocument();
- }
- }
The above code snippet will write XML file in single line i.e. not formatted, all content in same line.
- <?xml version="1.0" encoding="utf-8"?>
- <LumiaMobiles>
- <lumia550>
- <Height>136.1 mm</Height>
- <Width>67.8 mm</Width>
- <Thickness>9.9 mm</Thickness>
- <Weight>141.9 g</Weight>
- </lumia550>
- <lumia950>
- <Height>145 mm</Height>
- <Width>73.2 mm</Width>
- <Thickness>8.2 mm</Thickness>
- <Weight>150 g</Weight>
- </lumia950>
- <Text>
- <Height />
- <Width />
- <Thickness>8.2 mm</Thickness>
- <Weight>150 g</Weight>
- </Text>
- </LumiaMobiles>
So to write formatted XML data we can use XmlTextWriter,
Write XML File Using Xml TextWriter,
- public void WriteXMLFileUsingXmlTextWriter()
- {
- List < LumiaMobile > LumiaMobiles = new List < LumiaMobile > ();
- LumiaMobiles.Add(new LumiaMobile
- {
- ModelId = "lumia550",
- Height = "136.1 mm",
- Width = "67.8 mm",
- Thickness = "9.9 mm",
- Weight = "141.9 g"
- });
- LumiaMobiles.Add(new LumiaMobile
- {
- ModelId = "lumia950",
- Height = "145 mm",
- Width = "73.2 mm",
- Thickness = "8.2 mm",
- Weight = "150 g"
- });
- LumiaMobiles.Add(new LumiaMobile
- {
- ModelId = "Text",
- Height = "",
- Width = "",
- Thickness = "8.2 mm",
- Weight = "150 g"
- });
- string filename = "LumiaMobiles.xml";
- using(XmlTextWriter writer = new XmlTextWriter(new StreamWriter(new FileStream(filename, FileMode.Create))))
- {
- writer.Formatting = Formatting.Indented;
- writer.Indentation = 4;
- writer.WriteStartDocument();
- writer.WriteStartElement("LumiaMobiles");
- foreach(LumiaMobile lumiaMobile in LumiaMobiles)
- {
- writer.WriteStartElement(lumiaMobile.ModelId);
- writer.WriteElementString("Height", lumiaMobile.Height);
- writer.WriteElementString("Width", lumiaMobile.Width);
- writer.WriteElementString("Thickness", lumiaMobile.Thickness);
- writer.WriteElementString("Weight", lumiaMobile.Weight);
- writer.WriteEndElement();
- }
- writer.WriteEndElement();
- writer.WriteEndDocument();
- }
- }
It will write XML file as:
- <?xml version="1.0" encoding="utf-8"?>
- <LumiaMobiles>
- <lumia550>
- <Height>136.1 mm</Height>
- <Width>67.8 mm</Width>
- <Thickness>9.9 mm</Thickness>
- <Weight>141.9 g</Weight>
- </lumia550>
- <lumia950>
- <Height>145 mm</Height>
- <Width>73.2 mm</Width>
- <Thickness>8.2 mm</Thickness>
- <Weight>150 g</Weight>
- </lumia950>
- <Text>
- <Height />
- <Width />
- <Thickness>8.2 mm</Thickness>
- <Weight>150 g</Weight>
- </Text>
- </LumiaMobiles>
Now use the below code to Read the above XML File,
- public void ReadXMLFile(string xmlFileName)
- {
- XmlDocument xmlDocument = new XmlDocument();
- xmlDocument.Load(xmlFileName);
- XmlNodeList nodeList = xmlDocument.SelectNodes("LumiaMobiles");
- foreach(XmlNode node in nodeList)
- {
- XmlNodeList lumianodes = node.ChildNodes;
- foreach(XmlNode item in lumianodes)
- {
- Console.WriteLine("ModelID : {0}", item.Name);
- Console.WriteLine("Height: {0}", item.SelectSingleNode("Height").InnerText);
- Console.WriteLine("Width: {0}", item.SelectSingleNode("Width").InnerText);
- Console.WriteLine("Thickness: {0}", item.SelectSingleNode("Thickness").InnerText);
- Console.WriteLine("Weight: {0}", item.SelectSingleNode("Weight").InnerText);
- Console.WriteLine();
- }
- }
- Console.ReadKey();
- }