For the meanings and differences between XmlNode and XmlElement in XML, xmlnodexmlelement
I learned XML today and met two classes, XmlNode and XmlElement, so I have the following:
Today, two classes are found in the process of operating XML documents using ASP. NET: XmlNode and XmlElement. The functions of these two classes are extremely similar (because we generally operate on Element nodes ). The answer is the same for a long time. There will never be any important points. Some people have very vague answers. In desperation, they set their own breakpoint to try to compare the results. The following is a summary:
1. the inheritance relationship XmlElement inherits from the xmlworker node and the XmlNode class. It can be seen that the XmlElement is a subclass of the XmlNode. We know that the subclass inherits all attributes and methods of the parent class. Therefore, the methods and attributes of XmlNode can also be used in the XmlElement class.
2. XmlElement is a special XmlNode class. Xml nodes have multiple types: attribute nodes, comment nodes, text nodes, and element nodes. That is, XmlNode is the collective name of these nodes. However, XmlElement specifically refers to element nodes.
3. XmlElement is an existing class that can be directly instantiated. XmlNode is an abstract class and must be created through the XmlDocument instance through CreateNode.
4. XmlElement has many Attribute operation methods, allowing you to conveniently perform read and write operations on its Attributes (XmlNode can also obtain the Attribute list through the Attributes Attribute ).
5. Some people commented on the differences between the two classes on the Internet: XmlNode contains subnodes, and XmlElement only contains attributes and itself does not contain subnodes. However, I tested the code and found that even the XmlElement can obtain the subnode list through ChildNodes. Although in breakpoint interrupt mode, I did not find that the XmlElement has the ChildNodes attribute. Example:
<Book img = "aspnet.jpg"> // xmlnodeand xmlelement, but imgand aspnet.jpg are only xmlnodes.
<Name> // both XmlNode and XmlElement
C # // only XmlNode
</Name>
</Book>
That is, an element node can be converted to an XmlElement. In this way, both the XmlNode function and the attribute operation method can be used.
Any Node can use XmlNode because XmlNode is the parent class of all nodes.
This is just a piece of my personal experience. I hope that our predecessors will correct the content or something that needs to be supplemented.
Reprinted from http://bbs.csdn.net/topics/330203920
The file name for creating XML is cjp. xml.
<? Xml version = "1.0" encoding = "UTF-8"?> // The document header. Comments or code that cannot be added before the Document Header
<Lanou> // open the label <Name> the lanou is the root node, and the root node has only one
<! -- Note: Teacher array subnode --> // note form: <! -- Comment content -->
<Teachers> // <teachers> subnode, which contains multiple <teacher> nodes. Therefore, we can consider it as a teachers array.
<! -- Every teacher -->
<Teacher>
<! -- Instructor Attributes -->
<Name> er Hao </name>
<Age> 34 </age>
</Teacher>
<Teacher>
<Name> Yi huiyun </name>
<Age> 31 </age>
<Class> 39 </class>
</Teacher>
<Teacher>
<Name> Xu Mingxin </name>
<Age> 30 </age>
<Wife>
<Name> Lina </name>
</Wife>
</Teacher>
</Teachers>
</Lanou> // close the tag </Name>
Using UnityEngine;
Using System. Collections;
// Process the XML structure document
// 1. import data to the database
Using System. Xml;
The following code is written in unity:
Public class DataManager: MonoBehaviour {
// Function
Void Load ()
{
// 1. Use the XmlDocument object in the XML library
XmlDocument xmldoc = new XmlDocument (); // you can read the documents in the xml library.
// Use the xmldoc object to load the document string of the xml structure
Xmldoc. LoadXml ("<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> <Lanou> lanou </lanou> ");
TextAsset ta = Resources. Load ("cjp") as TextAsset;
Xmldoc. LoadXml (ta. text );
// Xmldoc. Load ("Resources/cjp. xml ");
// Each XML document contains only one root node.
XmlElement root = xmldoc. DocumentElement;
// Find the first of all teacher lists
// XmlNodeList list = xmldoc. SelectNodes ("// teacher [1]");
// XmlNodeList list = xmldoc. SelectNodes ("// teacher [wife]"); // XmlNodeList list = xmldoc. SelectNodes ("// teacher [age = 30]");
// XmlNodeList list = xmldoc. SelectNodes ("// teacher [lase ()]"); // The Last
// Foreach (XmlNode node in list ){
// Debug. Log (node. Name + ":" + node. InnerText );//
}
Debug. Log (root. InnerText );
// Debug. Log (root. Name );
}
Void Start (){
Load ();
}