In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what is the basic usage of XML in C#. It is very detailed and has certain reference value. Friends who are interested must finish it!
What is XML?
XML: extensible markup language.
The role of XML:
Plain text, strong compatibility.
The difference from HTML:
Xml: mainly used to process and store data. There is no prescribed label and can be expanded.
Html: the display and description of data. The syntax label is fixed.
XML syntax features:
Case sensitive.
There can be only one root node.
Labels appear in pairs.
Attributes are in double quotation marks.
There is no reservation label. Write what you use.
Document statement:
Note:
CDATA: original text
Xmldocument operation: class Program {static void Main (string [] args) {/ / write xml / /, build Dom object XmlDocument xmlDoc = new XmlDocument () in memory; / / add documentation description XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration ("1.0", "utf-8", "yes") XmlDoc.AppendChild (xmlDeclaration); / / add root element / / create root element XmlElement rootElement = xmlDoc.CreateElement ("school"); xmlDoc.AppendChild (rootElement); / / 3, add child elements, then add child elements to XmlElement xmlClassElement = xmlDoc.CreateElement ("class") under the rootElement node / / add the id attribute XmlAttribute attr = xmlDoc.CreateAttribute ("id") for the class element; attr.Value = "X01"; xmlClassElement.Attributes.Append (attr); rootElement.AppendChild (xmlClassElement); / / 4. Create the student node for the class. XmlElement xmlStudentElement = xmlDoc.CreateElement ("student"); / / add the sid attribute to the student element. XmlAttribute studentAttr = xmlDoc.CreateAttribute ("sid"); studentAttr.Value = "s011"; xmlStudentElement.Attributes.Append (studentAttr); xmlClassElement.AppendChild (xmlStudentElement); / / add name node to student. XmlElement xmlNameElement = xmlDoc.CreateElement ("name"); xmlNameElement.InnerText = "day"; xmlStudentElement.AppendChild (xmlNameElement); / / 2. Write the Dom object to the xml file xmlDoc.Save ("school.xml"); Console.WriteLine ("ok");}}
The above methods can be written in a loop.
Xdocument operation. Class Program {static void Main (string [] args) {/ / write the file List list = new List () through xdocument; list.Add (new Person () {Name = "Sam", Age = 18}); list.Add (new Person () {Name = "Penny", Age = 20}); / / 1, create the Dom object. XDocument xDoc = new XDocument (); XDeclaration xDec = new XDeclaration ("1.0", "utf-8", null); / / set document definition xDoc.Declaration = xDec; / / 2, create root node XElement rootElement = new XElement ("List"); xDoc.Add (rootElement) / / 3. Loop creation node for (int I = 0; I < list.Count; iTunes +) {XElement PersonElement = new XElement ("Person"); PersonElement.SetAttributeValue ("id", (I + 1). ToString ()); PersonElement.SetElementValue ("Name", list.Name) PersonElement.SetElementValue ("Age", list [I] .Age); rootElement.Add (PersonElement);} xDoc.Save ("List.xml"); Console.WriteLine ("ok");}} class Person {public string Name {get; set;} public int Age {get; set }} class Program {static void Main (string [] args) {/ / read the XML file. XDocument document = XDocument.Load ("List.xml"); XElement rootElement = document.Root; Console.WriteLine ("subscriber: {0}", rootElement.Element ("CustomerName") .value); foreach (var item in rootElement.Element ("Items"). Elements ("OrderItem") {Console.WriteLine ("Commodity name: {0}", item.Attribute ("Name"). Value) } these are all the contents of the article "what are the basic uses of XML in C#". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.