You are on page 1of 3

using System;

using System.Collections.Generic;
using System.ComponentModel;
//using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Collections;
namespace XML_ReadWrite
{
class Program
{
static void Main(string[] args)
{
// calea executabilului in WinForms:
// string xmlPath = Application.StartupPath + "\\Test.xml";
// calea executabilului in Console Application:
string xmlPath = System.IO.Path.GetDirectoryName(System.Reflection.A
ssembly.GetExecutingAssembly().GetName().CodeBase).ToString();
string xmlPathRead = xmlPath + "\\TestRead.xml";
xmlPathRead = xmlPathRead.Remove(0, 6);
if (File.Exists(xmlPathRead))
{
Console.WriteLine("---------\nREAD XML\n---------");
XmlDocument xmlDocRead = new XmlDocument();
xmlDocRead.Load(@xmlPathRead);
// pentru parsarea declaratie XML se va parsa sirul intre primel
e '<' si '?>'
string xmlAllRead = xmlDocRead.OuterXml; // tot XML-ul
Console.WriteLine(xmlAllRead);
XmlElement N1_L0 = xmlDocRead.DocumentElement; //root
string innerText_N1_L0 = N1_L0.InnerText;
Console.WriteLine(innerText_N1_L0);
XmlNodeList N1_L1 = xmlDocRead.DocumentElement.SelectNodes("to")
; //nod to
string innerText_N1_L1 = N1_L1[0].InnerText;
Console.WriteLine(innerText_N1_L1);
XmlNodeList N1_L2_a = xmlDocRead.DocumentElement.SelectNodes("TE
ST"); //nod TEST
string innerText_N1_L2_a = N1_L2_a[0].InnerText;
// verificare daca exista un anumit nod in XML
try
{
XmlNodeList N1_L2 = xmlDocRead.DocumentElement.SelectNodes("
TEST"); //nod TEST
string innerText_N1_L2 = N1_L2[0].InnerText;
Console.WriteLine(innerText_N1_L2);
}
catch (Exception ex)
{
Console.WriteLine("NU - exista nodul TEST in XML.");
};
XmlNodeList N2_L1 = xmlDocRead.DocumentElement.SelectNodes("from
"); // nod from
string innerText_N2_L1 = N2_L1[0].InnerText;
Console.WriteLine(innerText_N2_L1);
XmlNodeList N3_L1 = xmlDocRead.DocumentElement.SelectNodes("mail
"); // nod mail
string attrText_N4_L2 = N3_L1[0].SelectNodes("title")[0].Attribu
tes["attr_1"].Value;// nod title
string innerText_N4_L2 = N3_L1[0].SelectNodes("title")[0].InnerT
ext;
Console.WriteLine(attrText_N4_L2 + " " + innerText_N4_L2);
string innerText_N5_L2 = N3_L1[0].SelectNodes("body")[0].InnerTe
xt;// nod body 1
XmlNodeList N6_L3 = N3_L1[0].SelectNodes("body")[0].ChildNodes;
string innerText_N7_L3 = N6_L3[0].InnerText;// nod subject 1
Console.WriteLine(innerText_N7_L3);
string innerText_N8_L3 = N6_L3[1].InnerText;// nod message 1
Console.WriteLine(innerText_N8_L3);
string innerText_N9_L2 = N3_L1[0].SelectNodes("body")[1].InnerTe
xt;// nod body 2
XmlNodeList N10_L3 = N3_L1[0].SelectNodes("body")[1].ChildNodes;
string innerText_N11_L3 = N10_L3[0].InnerText;// nod subject 2
Console.WriteLine(innerText_N11_L3);
string innerText_N12_L3 = N10_L3[1].InnerText;// nod message 2
Console.WriteLine(innerText_N12_L3);
Console.WriteLine("---------\nWRITE XML\n---------");
XmlDocument xmlDocWrite = new XmlDocument();
XmlDeclaration xmlDeclaration = xmlDocWrite.CreateXmlDeclaration
("1.0", "ISO-8859-1", null);
xmlDocWrite.AppendChild(xmlDeclaration);
// numele atichetelor si atributelor trebuie sa fie fara spatii
XmlNode productsNode = xmlDocWrite.CreateElement("products");
xmlDocWrite.AppendChild(productsNode);
XmlNode productNode = xmlDocWrite.CreateElement("product");
XmlAttribute productAttribute = xmlDocWrite.CreateAttribute("id"
);
productAttribute.Value = "01";
productNode.Attributes.Append(productAttribute);
productsNode.AppendChild(productNode);
XmlNode nameNode = xmlDocWrite.CreateElement("Name");
nameNode.AppendChild(xmlDocWrite.CreateTextNode("Java"));
productNode.AppendChild(nameNode);
XmlNode priceNode = xmlDocWrite.CreateElement("Price");
priceNode.AppendChild(xmlDocWrite.CreateTextNode("Free"));
productNode.AppendChild(priceNode);
string xmlPathWrite = xmlPath + "\\TestWrite.xml";
xmlPathWrite = xmlPathWrite.Remove(0, 6);
xmlDocWrite.Save(@xmlPathWrite);
string xmlAllWrite = xmlDocWrite.OuterXml; // tot XML-ul
Console.WriteLine(xmlAllWrite);
}
else
{
Console.WriteLine("Nu exista fisierul XML!");
};
/*
String s = "SELECT [resource_id] FROM [table] WHERE [resource_name]
IN (";
s = s + "mere,pere,gutui,";
s = s.Remove(s.Length-1);
s = s + ")";
Console.WriteLine(s);
ArrayList arr = new ArrayList();
arr.Add("mere"); arr.Add("pere");
Console.WriteLine(arr[1]);
*/
}
}
}

You might also like