Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What should you pay attention to when operating XML files in C #

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what to pay attention to when operating XML files in C #". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what to pay attention to when operating XML files in C#"!

To manipulate the XML file, declare the following namespaces:

Using System.Xml

Learning Comprehensive experience of operating XML File in C # 1. Check whether the specified XML file exists

System.IO.File.Exists (file path and name)

Comprehensive experience of operating XML files in C # II. Creating XML files by using C # programming

I found two pieces of code on the Internet:

C # operating XML file to learn comprehensive experience code 1:

XmlDocument xmldoc = new XmlDocument (); XmlNode xmlnode; XmlElement xmlelem; XmlElement xmlelem2; XmlText xmltext; / add the declarative paragraph xmlnode = xmldoc.CreateNode (XmlNodeType.XmlDeclaration, "", "); xmlnode.InnerText+=" encoding=\ "GB2312\"; xmldoc.AppendChild (xmlnode); / / add a root element xmlelem = xmldoc.CreateElement ("", "ROOT", ""); xmltext = xmldoc.CreateTextNode ("Root Text"); xmlelem.AppendChild (xmltext) Xmldoc.AppendChild (xmlelem); / / add another element xmlelem2 = xmldoc.CreateElement ("SampleElement"); xmlelem2 = xmldoc.CreateElement ("", "SampleElement", ""); xmltext = xmldoc.CreateTextNode ("The text of the sample element"); xmlelem2.AppendChild (xmltext); xmldoc.ChildNodes.Item (1) .AppendChild (xmlelem2); / / Save the created XML document try {xmldoc.Save ("data.xml") } catch (Exception f) {/ / display error message MessageBox.Show (f.Message);} / / Console.ReadLine ()

This code successfully tested in win2003ser+vs2005 environment, but the XML file format is very messy, I do not know how to adjust the format, friends who know, please let me know.

Operate the XML file on behalf of C# to learn the complex code II:

String FileName = Application.StartupPath+ "\\ phone.xml"; XmlTextWriter objXmlTextWriter = new XmlTextWriter (FileName,Encoding.Default); objXmlTextWriter.Formatting = Formatting.Indented; objXmlTextWriter.Indentation = 6; objXmlTextWriter.WriteStartDocument (); objXmlTextWriter.WriteStartElement ("," PhoneBook ","); objXmlTextWriter.WriteStartElement ("," Name ","); objXmlTextWriter.WriteString ("Garfield"); objXmlTextWriter.WriteEndElement (); objXmlTextWriter.WriteStartElement ("", "Number", "") ObjXmlTextWriter.WriteString ("5555555"); objXmlTextWriter.WriteEndElement (); objXmlTextWriter.WriteStartElement ("", "City", "); objXmlTextWriter.WriteString (" New York "); objXmlTextWriter.WriteEndElement (); objXmlTextWriter.WriteStartElement ("," DateOfBirth ","); objXmlTextWriter.WriteString (" 26 DateOfBirth "); objXmlTextWriter.WriteEndElement (); objXmlTextWriter.WriteEndElement (); objXmlTextWriter.WriteEndDocument (); objXmlTextWriter.Flush (); objXmlTextWriter.Close ()

This code passed the test in the win2003ser+vs2005 environment, the effect is very good, but also relatively easy to understand, I usually use this code to create XML files.

Comprehensive experience of C # operating XML file III. Read and modify the value of a node in XML file

String path = "phone.xml"; XmlDocument doc = new XmlDocument (); doc.Load (path); / / read all node tables XmlNamespaceManager xnm = new XmlNamespaceManager (doc.NameTable); / / read node value XmlNode node = doc. SelectSingleNode ("/ PhoneBook/Name", xnm); / / node.InnerText is the value read out / / modify node value node.InnerText= "content to be modified"; / / save the modified content doc.Save (path); at this point, I believe you have a deeper understanding of "C# operation XML file should pay attention to what", might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report