C #-XML-data transmission,
Http://www.cnblogs.com/fengxuehuanlin/p/5631664.html
Xml is a very important thing. During normal development, the main thing about this content is to master the reading and writing operations of XML content.
Xml can be used as a small database to store data.
Html is mainly used to display data. It is designed at the front-end of XAML.
1. What is XML?
- XML refers to the EXtensible Markup Language (EXtensible Markup Language)-EXtensible means that tags can be customized, Unlike html fixed tags.
- XML is a markup language, similar to HTML
- XML is designed to transmit data rather than display data.
- The XML tag is not predefined. You need to define the tag.
- XML is designed to be self-descriptive
- XML is W3C recommendation Standard
Ii. XML Syntax:
1. an XML file contains the following parts:
Document declaration, elements, attributes, comments, CDATA (special characters), processing commands, node nodes
- Nodes contain elements (all the things seen in the text are nodes, and the elements refer to labels ).
- Only one root node is allowed.
2. The simplest declaration format
<? Xml version = "1.0"?> Declare the document encoding using the encoding attribute <? Xml version = "1.0" encoding = "UTF-8"?> Use the standalone attribute to indicate whether the document is independent <? Xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
3. write XML content:
- Dom-document object model (XmlDocument object). Note that there must be a root node first.
// Use code to create an XML document // 1. Reference The namespace // 2. Create an XML Document Object XmlDocument doc = new XmlDocument (); // 3. create the first line of description information and add it to the doc file XmlDeclaration dec = doc. createXmlDeclaration ("1.0", "UTF-8", null); doc. appendChild (dec); // 4. create the root node XmlElement siemens = doc. createElement ("Siemens"); doc. appendChild (siemens); // 5. create a sub-node XmlElement opcua = doc for the root node books. createElement ("OPCUA"); siemens. appendChild (opcua); // 6. add the sub-node XmlElement plc1 = doc to book1. createElement ("PLC1"); plc1.InnerText = "_ 84303"; opcua. appendChild (plc1); XmlElement plc2 = doc. createElement ("PLC2"); plc2.InnerText = "_ 87303"; opcua. appendChild (plc2); XmlElement plc3 = doc. createElement ("PLC3"); plc3.InnerText = "_ 89303"; opcua. appendChild (plc3); XmlElement plc4 = doc. createElement ("PLC4"); plc4.InnerXml = "<a> I will send instead of </a>"; opcua. appendChild (plc4); XmlElement plc4_1 = doc. createElement ("Grade"); plc4_1.SetAttribute ("name", "James"); plc4_1.InnerXml = "preschool"; plc4.AppendChild (plc4_1); doc. save ("Siemens. xml ");
XmlDocument doc2 = new XmlDocument(); XmlDeclaration des2 = doc2.CreateXmlDeclaration("1.0","utf-8",null); doc2.AppendChild(des2); XmlElement person = doc2.CreateElement("Person"); doc2.AppendChild(person); for(int i=0;i<lstudent.Count;i++) { XmlElement student = doc2.CreateElement("Studemt"); student.SetAttribute("ID",lstudent[i].id.ToString()); XmlElement name = doc2.CreateElement("Name"); name.InnerXml = lstudent[i].name; XmlElement age = doc2.CreateElement("Age"); age.InnerXml = lstudent[i].age.ToString(); XmlElement gender = doc2.CreateElement("Gender"); gender.InnerXml = lstudent[i].gender.ToString(); person.AppendChild(student); student.AppendChild(name); student.AppendChild(age); student.AppendChild(gender); } doc2.Save("student.xml");
4. Read XML files:
Xml file content:
<? Xml version = "1.0" encoding = "UTF-8"?> <Library id = "30"> <BOOK id = "20"> <name> advanced mathematics </name> <name1> College English </name1> </BOOK> </library>
Read XML content:
Static void Main (string [] args) {// load the XML file into XDocument document = XDocument. load ("D :\\ 123.xml"); // obtain the XML root element and perform operations XElement root = document. root; XElement ele = root. element ("BOOK"); // obtain the value of the name tag XElement shuxing = ele. element ("name"); Console. writeLine (shuxing. value); // obtain all the sub-elements under the root element, IEnumerable <XElement> enumerable = root. elements (); foreach (XElement item in enumerable) {foreach (XElement item1 in item. elements () {Console. writeLine (item1.Name); // output name name1} Console. writeLine (item. attribute ("id "). value); // output 20} Console. readKey ();}
5. add, delete, modify, and query
# Add, delete, modify, and query XmlDocument doc3 = new XmlDocument (); if (File. exists ("student. xml ") {// exist // load the doc3.Load file (" student. xml "); // append, first obtain the root node XmlElement Person = doc3.DocumentElement; # Add the region // append the subnode // XmlElement student = doc3.CreateElement (" Student "); // student. setAttribute ("ID", "5"); // Person. appendChild (student); // XmlElement ignore = doc3.CreateElement ("..... "); // student. appendChild (ignore); # endregion # region modify query // modify query 1 -- Obtain the subnode method // XmlNodeList ndoes = Person. childNodes; // foreach (XmlNode xnode in ndoes) // {// does not contain attributes // Console. writeLine (xnode. innerText); // Property Information // Console. writeLine (xnode. attributes ["ID"]. value); // if (xnode. attributes ["ID"]. value = "3") // {// xnode. attributes ["ID"]. value = "modify"; //} // check whether the xmlpath method is XmlNode node = Person. selectSingleNode ("/Person/Student"); Console. writeLine (node. innerText); // directly find a subnode XmlNode node1 = Person. selectSingleNode ("/Person/Student [@ ID = '2']/Gender"); node1.InnerText = "male"; Console. writeLine (node1.InnerText); Console. readKey (); # modify endregion check # Delete region // The root node cannot be deleted // doc3.RemoveAll (); // The removed node must be a subset of the Operation node XmlNode node2 = Person. selectSingleNode ("/Person/Student [@ ID = '2']/Gender"); // Person. removeChild (node2); XmlNode nodep = Person. selectSingleNode ("/Person/Student [@ ID = '2']/"); nodep. removeChild (node2); // remove the nodep attribute. attributes. removeNamedItem ("ID"); # endregion} else {// XmlDeclaration des3 = doc3.CreateXmlDeclaration ("1.0", "UTF-8", null); doc3.AppendChild (des3 ); // Add the root node XmlElement person3 = doc3.CreateElement ("Person"); doc3.AppendChild (person3);} doc3.Save ("student. xml "); # endregion