Form1.cs 8.96 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.Windows.Forms;

namespace ModifyXml
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string selectedxmlfile;
            selectedxmlfile = comboBox1.Text;
            string appPath = Path.GetDirectoryName(Application.ExecutablePath);
            int strlen = appPath.Length;

            if (selectedxmlfile == "da_dat_brview.xml")
            {
                string xmldocpath = appPath.Substring(0, strlen - 10) + @"\Xml\da_dat_brview.xml";
                var document2 = XDocument.Load(xmldocpath);
                try
                {
                     var query2 = from c2 in document2.Descendants("response").Elements("it")
                                select c2;
                     foreach (XElement layer2 in query2)
                     {   //remove attributes
                         layer2.Attribute("zr").Remove();
                         layer2.Attribute("cnb").Remove();

                         //to Change attributes names first get value 
                         var attvoId_value = layer2.Attribute("voId").Value;
                         var attbrId_value = layer2.Attribute("brId").Value;
                         var attzm_value = layer2.Attribute("zm").Value;
                         var attox_value = layer2.Attribute("ox").Value;
                         var attoy_value = layer2.Attribute("oy").Value;
                         var attwd_value = layer2.Attribute("wd").Value;
                         var attht_value = layer2.Attribute("ht").Value;
                         var attismr_value = layer2.Attribute("ismr").Value;
                         var attmro_value = layer2.Attribute("mro").Value;
                         var attisp_value = layer2.Attribute("isp").Value;

                         //remove attribute we want to rename
                         layer2.Attribute("voId").Remove();
                         layer2.Attribute("brId").Remove();
                         layer2.Attribute("zm").Remove();
                         layer2.Attribute("ox").Remove();
                         layer2.Attribute("oy").Remove();
                         layer2.Attribute("wd").Remove();
                         layer2.Attribute("ht").Remove();
                         layer2.Attribute("ismr").Remove();
                         layer2.Attribute("mro").Remove();
                         layer2.Attribute("isp").Remove(); 

                         //add renamed attributes 
                         layer2.SetAttributeValue("ViewOrientationId", attvoId_value);
                         layer2.SetAttributeValue("BodyRegionId", attbrId_value);
                         layer2.SetAttributeValue("Zoom", attzm_value);
                         layer2.SetAttributeValue("X", attox_value);
                         layer2.SetAttributeValue("Y",attoy_value);
                         layer2.SetAttributeValue("Width", attwd_value);
                         layer2.SetAttributeValue("Height", attht_value);
                         layer2.SetAttributeValue("HaveMirrorImage", attismr_value);
                         layer2.SetAttributeValue("MirrorX", attmro_value);
                         layer2.SetAttributeValue("IsPrimary", attisp_value);

                         //rename the element it
                         XName newElemName = "BodyRegionCordinates";
                         layer2.Name = newElemName;
                     }
                     var doc2root = document2.Element("response");
                     XName newdoc2root = "BodyRegionViews";
                     doc2root.Name = newdoc2root;

                     //appPath.Substring(0, strlen - 10) + @"\Xml\ModifiedXml\da_dat_brview.xml";
                     document2.Save(appPath.Substring(0, strlen - 10) + @"\Xml\ModifiedXml\da_dat_brview.xml");

                     MessageBox.Show(selectedxmlfile + " Modified and saved in ModifyXml Folder");
                }
                catch (Exception exp)
                {
                    exp.ToString();
                }

            }

            else
            {
                //Modify the xmls  da_dat_layer_(1 to 10).xml
                string xmldocpath = appPath.Substring(0, strlen - 10) + @"\Xml\" + selectedxmlfile;
                var document = XDocument.Load(xmldocpath);
                try
                {
                    var query = from c in document.Descendants("response").Elements("dl")
                                select c;
                    foreach (XElement layer in query)
                    {   //remove attributes lni,lnm and isbs (this is commented as per new changes suggested by Amrita on 30thAug2016

                       // layer.Attribute("lni").Remove();
                       // layer.Attribute("lnm").Remove();
                       // layer.Attribute("isbs").Remove();

                        //Change ln and ssk attributes names
                        var attln_value = layer.Attribute("ln").Value;
                        var attissk_value = layer.Attribute("issk").Value;
                        //remove attribute we want to rename
                        layer.Attribute("ln").Remove();
                        layer.Attribute("issk").Remove();
                        //add renamed attributes 
                        layer.SetAttributeValue("LayerNumber", attln_value);
                        layer.SetAttributeValue("HaveSkinTone", attissk_value);

                        //go to the next child element
                        if (layer.HasElements == true)
                        {
                            var chElements = layer.Elements("map").ToList();
                            foreach (XElement chelm in chElements)
                            {
                                //(this is commented as per new changes suggested by Amrita on 30thAug2016
                               // chelm.Attribute("lni").Remove();

                                //Change brId attributes names
                                var chattbrId_value = chelm.Attribute("brId").Value;
                                chelm.Attribute("brId").Remove();
                                chelm.SetAttributeValue("BodyRegionId", chattbrId_value);

                                //change element for childnode
                                if (chelm.HasElements == true)
                                {
                                    var subchdElement = chelm.Element("art");
                                    //(this is commented as per new changes suggested by Amrita on 30thAug2016
                                    //remove isoid attribute
                                    //  subchdElement.Attribute("isoId").Remove();

                                    //change attribute name
                                    var subchdattsktn_value = subchdElement.Attribute("sktn").Value;
                                    var subchdattzm_value = subchdElement.Attribute("zm").Value;
                                    var subchdattcompId_value = subchdElement.Attribute("compId").Value;

                                    subchdElement.Attribute("sktn").Remove();
                                    subchdElement.Attribute("zm").Remove();
                                    subchdElement.Attribute("compId").Remove();

                                    subchdElement.SetAttributeValue("SkintTone", subchdattsktn_value);
                                    subchdElement.SetAttributeValue("Zoom", subchdattzm_value);
                                    subchdElement.SetAttributeValue("ImageName", subchdattcompId_value);

                                    XName newsubchdElem = "Image";
                                    subchdElement.Name = newsubchdElem;

                                }

                                XName newchElem = "BodyRegion";
                                chelm.Name = newchElem;

                            }

                            XName newElem = "DataLayer";
                            layer.Name = newElem;
                        }

                    }
                    var docroot = document.Element("response");
                    XName newdocroot = "Layers";
                    docroot.Name = newdocroot;

                    document.Save(appPath.Substring(0, strlen - 10) + @"\Xml\ModifiedXml\" + selectedxmlfile);

                    MessageBox.Show(selectedxmlfile + " Modified and saved in ModifyXml Folder");
                }
                catch (Exception exp)
                {

                    exp.ToString();
                }
                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            AAForm atlasAnatomyForm = new AAForm();
            atlasAnatomyForm.Show();
        }
    }
}