C # XML operations

Source: Internet
Author: User

In the previous chapter, we wrote XML using the XmlWriter class. However, for some situations, especially if doing updates of existing XML, using the XmlDocument class can come in handy . You should is aware of the higher memory consumption though, mainly for large XML documents. Here is some code:

UsingSystem;UsingSystem.Text;UsingSystem.Xml;UsingSystem.Xml.Serialization;Namespacewritingxml{Classprogram {Static voidMain (String[] args) {XmlDocument xmldoc= NewXmlDocument (); XmlNode RootNode=Xmldoc.createelement ("Users"); Xmldoc.appendchild (RootNode); XmlNode Usernode=Xmldoc.createelement ("User"); XmlAttribute attribute=Xmldoc.createattribute ("Age"); Attribute. Value= "42"; UserNode.Attributes.Append (attribute); Usernode.innertext= "John Doe"; Rootnode.appendchild (Usernode); Usernode=Xmldoc.createelement ("User"); Attribute=Xmldoc.createattribute ( "age "=  " Span class= "C#-stringdefaultstyle" >39 "; UserNode.Attributes.Append (attribute); Usernode.innertext =  "jane doe"  "test-doc.xml"  

And the resulting XML:

<Users>  <Userage= ">john doe</< Span class= "Html-tagnamestyle" >user> Span class= "Html-tagdelimiterstyle" ><user age= "" >jane doe</< Span class= "Html-tagnamestyle" >user>< Span class= "Html-tagdelimiterstyle" ></users >            

As you can see, this is a bit more object oriented than the XmlWriter approach, and it does require a bit more code. What we are instantiate an XmlDocument object, which we'll use to create both new elements and attributes, USI ng the createelement () and CreateAttribute () methods. Each of the append, the elements, either directly to the document, or to another element. You can see the example, where the root element ("users") are appended directly to the document, while the user ele ments is appended to the root element. Attributes is of course appended to the elements where they belong, using the Append () method on the Attributes property. The entire XML document is written to the disk in the last line, where we use the Save () method.

Now, as already mentioned, this requires a bit more code than when using the XmlWriter, but imagine a situation where you Just need to go into an existing XML document and change a few values. Using the XmlWriter approach, you would has the to first read all the information using a XmlReader, store it, change it, an D then write the entire information back using the XmlWriter. Because The XmlDocument holds everything in memory for your, updating an existing XML file becomes a lot simpler. The following example opens the "test-doc.xml" file that we created in the previous chapter and increases every user's age by one. See how easy it is:

UsingSystem;UsingSystem.Text;UsingSystem.Xml;UsingSystem.Xml.Serialization;Namespacewritingxml{Classprogram {Static voidMain (String[] args) {XmlDocument xmldoc= NewXmlDocument (); Xmldoc.load ("Test-doc.xml"); XmlNodeList Usernodes=Xmldoc.selectnodes ("Users/user");Foreach(XmlNode UsernodeInchUsernodes) {IntAge= int "age "". Value); Usernode.attributes[ "age" = (age + 1). ToString (); } xmldoc.save (@ "D:\test-doc.xml "); } }} 

We load the XML file and ask for all the <user> nodes. We then iterate through them, read the age of attribute into a integer variable, and then we write the value of the back to the nod E and attribute, after increasing the value by 1. At last, we save the document back to the same file and if your open it up, you'll see this our users all just had a Birt Hday. Pretty cool!

C # XML operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.