Asp.net operation xml (add, delete, query, and modify), asp. netxml

Source: Internet
Author: User

Asp.net operation xml (add, delete, query, and modify), asp. netxml

Asp.net operation xml

1. xml documentation Products. xml

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <products xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://www.pro.org/2001/products" xsi: schemaLocation = "http://www.pro.org/2001/products products. xsd "> 3 <item belong =" digital "> 4 <id> 1 </id> 5 <name> mobile phone </name> 6 <price> 1000 </price> 7 </item> 8 <item belong = "clothing"> 9 <id> 2 </id> 10 <name> men's wear </name> 11 <price> 200 </price> 12 </item> 13 <item belong = "food"> 14 <id> 3 </id> 15 <name> cucumber </name> 16 <price> 4 </price> 17 </item> 18 </products>

 

2. schema constraint document products. xml

 1 <?xml version="1.0" encoding="utf-8"?> 2 <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.pro.org/2001/products" xmlns:pro="http://www.pro.org/2001/products" elementFormDefault="qualified"> 3   <element name="products" type="pro:pro"></element> 4   <complexType name="pro"> 5     <sequence> 6       <element name="item" maxOccurs="unbounded"> 7         <complexType> 8           <sequence> 9             <element name="id" type="string"></element>10             <element name="name" type="string"></element>11             <element name="price">12               <simpleType>13                 <restriction base="float">14                   <maxExclusive value="10000"></maxExclusive>15                   <minInclusive value="0"></minInclusive>16                 </restriction>17               </simpleType>18             </element>19           </sequence>20           <attribute name="belong" type="string"></attribute>21         </complexType>22       </element>23     </sequence>24   </complexType>25 </schema>

3. Define the entity class DBPro. cs

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. web; 5 6 /// <summary> 7 // DBPro abstract description 8 /// </summary> 9 public class DBPro10 {11 string belong; 12 string id; 13 string name; 14 string price; 15 public DBPro (string belong, string id, string name, string price) 16 {17 this. belong = belong; 18 this. id = id; 19 this. name = name; 20 this. price = price; 21} 22 public string Belong23 {24 get {return belong;} 25 set {belong = value;} 26} 27 public string ID28 {29 get {return id ;} 30 set {id = value;} 31} 32 public string Name33 {34 get {return name;} 35 set {name = value ;} 36} 37 public string Price38 {39 get {return price;} 40 set {price = value;} 41} 42}

4. Create a new web form Defaut. aspx and write the core code in Default. aspx. cs.

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. web; 5 using System. web. UI; 6 using System. web. UI. webControls; 7 using System. xml; 8 9 public partial class _ Default: System. web. UI. page 10 {11 protected void Page_Load (object sender, EventArgs e) 12 {// select a method to test 13 // SearchXml (); 14 DBPro pro = new DBPro ("appliance ", "10", "TV", "3999"); 15 AddToXml (pro); 16 // UpdateOneXml (pro); 17 // DeleteXml ("10 "); 18} 19 /// <summary> 20 // traverse xml document 21 /// </summary> 22 /// <param name = "pro"> </param> 23 private void SearchXml () 24 {25 // extract xml document 26 XmlDocument xd = new XmlDocument (); 27 xd. load (System. web. httpContext. current. server. mapPath ("Products. xml "); 28 // get the root node 29 XmlNode root = xd. documentElement; 30 // get node list 31 XmlNodeList items = root. childNodes; 32 // traverse item 33 Response. write ("<pre>"); 34 foreach (XmlNode item in items) 35 {36 // output attribute 37 Response. write (item. attributes ["belong"]. name + "=" + item. attributes ["belong"]. innerText); 38 // traverse the output subnode 39 foreach (XmlNode p in item) 40 {41 Response. write (p. name + "=" + p. innerText); 42} 43} 44 Response. write ("</pre> "); 45} 46 // <summary> 47 // Add 48 // xml </summary> 49 // <param name = "pro"> </param> 50 private void AddToXml (DBPro pro) 51 {52 // extract xml document 53 XmlDocument xd = new XmlDocument (); 54 xd. load (System. web. httpContext. current. server. mapPath ("Products. xml "); 55 // get the root node 56 XmlNode root = xd. documentElement; 57 // create element 58 XmlElement newItem = xd. createElement ("item"); 59 XmlElement newID = xd. createElement ("id"); 60 XmlElement newName = xd. createElement ("name"); 61 XmlElement newPrice = xd. createElement ("price"); 62 // Configuration Parameter 63 newItem. setAttribute ("belong", pro. belong); 64 newID. innerText = pro. ID; 65 newName. innerText = pro. name; 66 newPrice. innerText = pro. price; 67 // assemble 68 root. appendChild (newItem); 69 newItem. appendChild (newID); 70 newItem. appendChild (newName); 71 newItem. appendChild (newPrice); 72 xd. save (System. web. httpContext. current. server. mapPath ("Products. xml ")); 73} 74 // <summary> 75 // modify an xml Item 76 // </summary> 77 // <param name = "pro"> </param> 78 private void UpdateOneXml (DBPro pro) 79 {80 // extract xml document 81 XmlDocument xd = new XmlDocument (); 82 xd. load (System. web. httpContext. current. server. mapPath ("Products. xml "); 83 // get the root node 84 XmlNode root = xd. documentElement; 85 // get the node list 86 XmlNodeList items = root. childNodes; 87 // traverse node list 88 foreach (XmlNode item in items) 89 {90 // traverse item 91 foreach (XmlNode p in item) 92 {93 if (p. name = "id" & p. innerText = pro. ID) 94 {95 item. attributes ["belong"]. innerText = pro. belong; 96 p. nextSibling. innerText = pro. name; 97 p. nextSibling. nextSibling. innerText = pro. price; 98} 99} 100} 101} 102 // <summary> 103 // delete an xml item 104 // </summary> 105 // <param name = "pro "> </param> 106 private void DeleteXml (string id) 107 {108 // extract xml document 109 XmlDocument xd = new XmlDocument (); 110 xd. load (System. web. httpContext. current. server. mapPath ("Products. xml "); 111 // get the root node 112 XmlNode root = xd. documentElement; 113 // get node list 114 XmlNodeList items = root. childNodes; 115 // traverse node list 116 foreach (XmlNode item in items) 117 {118 // traverse item119 foreach (XmlNode p in item) 120 {121 if (p. name = "id" & p. innerText = id) 122 {123 root. removeChild (item); 124} 125} 126} 127} 128}

Note that: When I use XMLDocument to add elements, I encountered the following problem:
When the root node has the xmlns attribute, if you do not specify xmlns or specify xmlns as null when using XMLDocument to create child elements, the child element will automatically have the xmlns = "" attribute

<Item belong = "Household Appliances" xmlns = "">
<Id> 10 </id>
<Name> TV </name>
<Price> 3999 </price>
</Item>
Cause:

When the parent node has the xmlns attribute, the child node must specify the xmlns attribute. Only when the xmnls attribute of the child node is the same as that of the parent node will the child node not display the xmlns attribute, in the end. shown in xml file

Solution:

XmlElement newItem = xd. CreateElement ("item", xd. DocumentElement. NamespaceURI );
XmlElement newID = xd. CreateElement ("id", xd. DocumentElement. NamespaceURI );
XmlElement newName = xd. CreateElement ("name", xd. DocumentElement. NamespaceURI );
XmlElement newNumber = xd. CreateElement ("number", xd. DocumentElement. NamespaceURI );

Specify the namespace for each lower-level node. Otherwise, the xmlns = "" attribute will still appear.

/// If you have any shortcomings or errors, please criticize and correct them!

 

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.