XML dynamic insert parent class ID subclass ID, xml ID

Source: Internet
Author: User

XML dynamic insert parent class ID subclass ID, xml ID

Since I wrote my blog for the first time and wrote something bad, please give me some advice,

To explain: it takes a lot of effort to insert and save xml data dynamically. At last, the emperor finally gave me the opportunity to share it with everyone, please give us more advice on the imperfect writing.

Database Table Structure

Automatic Generation of Categoryid GUid

CategoryName category name
CategoryIdentifies
ParentIdentifies parent ID

Sort sorting

Controller code:

Public ActionResult CreateCategory (CategoryForm model) {// defines and loads the node (root node) XElement rootNode = XElement from the xml file. load (HttpRuntime. appDomainAppPath + "Runtime/Category. xml "); IEnumerable <XElement> ele = rootNode. elements ("Controller "). elements ("Root"); XElement dnode = rootNode. elements ("Controller "). firstOrDefault (); XElement pElement = null; // judge whether there is a root node var count = 0 in xml; XElement Parentnode = ele. where (e => (string) e. attribute ("CategoryName") = model. categoryName ). firstOrDefault (); var CategoryIdentifies = 0.0; // determine whether the current node exists if (Parentnode = null) {// determine whether it is a parent node if (model. parentIdentifies = "0") {// obtain the number of parent nodes of the same level. count = ele. count (); if (count> 0) {CategoryIdentifies = Convert. toDouble (model. categoryIdentifies) + count; pElement = new XElement ("Root", new XAttribute ("CategoryName", model. categoryName), new XAttribute ("CategoryIdentifies", CategoryIdentifies), new XAttribute ("ParentIdentifies", "0"), new XAttribute ("sort", model. sort); dnode. add (pElement);} else {pElement = new XElement ("Root", new XAttribute ("CategoryName", model. categoryName), new XAttribute ("CategoryIdentifies", model. categoryIdentifies), new XAttribute ("ParentIdentifies", "0"), new XAttribute ("sort", model. sort); dnode. add (pElement) ;}} else {// obtain the number of points at the same level count = rootNode. descendants ("Root "). where (e => (string) e. attribute ("ParentIdentifies") = model. parentIdentifies ). count (); // obtain the var sElement = rootNode of the parent node. descendants ("Root "). where (e => (string) e. attribute ("CategoryIdentifies") = model. parentIdentifies ). firstOrDefault (); if (count = 0) {pElement = new XElement ("Root", new XAttribute ("CategoryName", model. categoryName), new XAttribute ("CategoryIdentifies", sElement. attribute ("CategoryIdentifies "). value + model. parentIdentifies), new XAttribute ("ParentIdentifies", model. parentIdentifies), new XAttribute ("sort", model. sort); sElement. add (pElement);} else {CategoryIdentifies = Convert. toDouble (sElement. attribute ("CategoryIdentifies "). value + model. parentIdentifies) + count; pElement = new XElement ("Root", new XAttribute ("CategoryName", model. categoryName), new XAttribute ("CategoryIdentifies", CategoryIdentifies), new XAttribute ("ParentIdentifies", model. parentIdentifies), new XAttribute ("sort", model. sort); sElement. add (pElement) ;}} var id = model. categoryId = Guid. newGuid (); // generate a GUID // Save the xml Modification Operation rootNode. save (HttpRuntime. appDomainAppPath + "Runtime/Category. xml "); return RedirectToAction (" CategoryList ");}

You can write the view by yourself. You can also write a tree according to your preferences,

Xml results:

Note that the rule is used for query.

<?xml version="1.0" encoding="utf-8"?><Category>  <Controller name="AdminPublish">    <Root CategoryName="etre" CategoryIdentifies="100" ParentIdentifies="0" sort="1">      <Root CategoryName="wet" CategoryIdentifies="100100" ParentIdentifies="100" sort="1">        <Root CategoryName="wetsryerset" CategoryIdentifies="100100100100" ParentIdentifies="100100" sort="1" />      </Root>      <Root CategoryName="wetsryer" CategoryIdentifies="100101" ParentIdentifies="100" sort="1" />      <Root CategoryName="wetsryersetewt" CategoryIdentifies="100102" ParentIdentifies="100" sort="1" />    </Root>  </Controller></Category>

 

I implemented a product category (linkage) query function.


 

Note that this is association. This association is queried Based on the ParentIdentifies field. Check the database table structure rules and xml rules. Someone may ask why the database is not operated directly but xml, the reason is very simple. I will not talk about it here. I hope it will help you. Thank you.

 

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.