Base operation about XMl file

This blog will continue The steps of migrating metadata from SP site 2010 to SP site 2013,and the below is about Base operation about XMl file

Here is the base operation about create xml file:

public class MetadataXML
    {
        public void addWebTag(string xmlFilePath,string webUrl)
        {
            int flagWeb = 0;
            XmlDocument myXmlDoc = new XmlDocument();
            myXmlDoc.Load(xmlFilePath);
            XmlNode rootNode = myXmlDoc.SelectSingleNode("Site");
            XmlNodeList webNodes = rootNode.ChildNodes;
            try
            {
                foreach (XmlNode webNode in webNodes)
                {
                    if (webNode.Attributes["Url"].Value.Equals(webUrl))
                    {
                        flagWeb = 1;
                    }
                }
            }
            catch { }
            if (flagWeb == 0)
            {
                XmlElement webElement = myXmlDoc.CreateElement("Web");
                webElement.SetAttribute("Url",webUrl);
                rootNode.AppendChild(webElement);
                myXmlDoc.Save(xmlFilePath);
            }
        }

        public void addDocumentTag(string xmlFilePath,string webUrl,string documentName,string metadataName)
        {
            int flagDocument = 0;
            XmlDocument myXmlDoc = new XmlDocument();
            myXmlDoc.Load(xmlFilePath);
            XmlNode rootNode = myXmlDoc.SelectSingleNode("Site");
            XmlNodeList webNodes = rootNode.ChildNodes;
            try
            {
                foreach (XmlNode webNode in webNodes)
                {
                    if (webNode.Attributes["Url"].Value.Equals(webUrl))
                    {
                        XmlNodeList documentNodes = webNode.ChildNodes;
                        foreach (XmlNode documentNode in documentNodes)
                        {
                            if (documentNode.Attributes["Name"].Value.Equals(documentName) && documentNode.Attributes["Metadata"].Value.Equals(metadataName))
                            {
                                flagDocument = 1;
                            }
                        }
                    }
                }
            }
            catch { }
            if (flagDocument == 0)
            {
                foreach (XmlNode webNode in webNodes)
                {
                    if (webNode.Attributes["Url"].Value.Equals(webUrl))
                    {
                        XmlElement documentElement = myXmlDoc.CreateElement("Document");
                        documentElement.SetAttribute("Name",documentName);
                        documentElement.SetAttribute("Metadata",metadataName);
                        webNode.AppendChild(documentElement);
                        myXmlDoc.Save(xmlFilePath);
                    }
                }
            }
        }

        public void addFolderTag(string xmlFilePath,String documentName,string metadataName,string relativeUrl)
        {
            int flagFolder = 0;
            XmlDocument myXmlDoc = new XmlDocument();
            myXmlDoc.Load(xmlFilePath);
            XmlNode rootNode = myXmlDoc.SelectSingleNode("Site");
            XmlNodeList webNodes = rootNode.ChildNodes;
            try
            {
                foreach (XmlNode webNode in webNodes)
                {
                    if (webNode.Attributes["Url"].Value.Equals(webUrl))
                    {
                        XmlNodeList documentNodes = webNode.ChildNodes;
                        foreach (XmlNode documentNode in documentNodes)
                        {
                            if (documentNode.Attributes["Name"].Value.Equals(documentName) && documentNode.Attributes["Metadata"].Value.Equals(metadataName))
                            {
                                XmlNodeList folderNodes = documentNode.ChildNodes;
                                foreach (XmlNode folderNode in folderNodes)
                                {
                                    if (folderNode.Attributes["RelativeUrl"].Value.Equals(relativeUrl))
                                    {
                                        flagFolder = 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch { }
            if (flagFolder == 0)
            {
                foreach (XmlNode webNode in webNodes)
                {
                    if (webNode.Attributes["Url"].Value.Equals(webUrl))
                    {
                        XmlNodeList documentNodes = webNode.ChildNodes;
                        foreach (XmlNode documentNode in documentNodes)
                        {
                            if (documentNode.Attributes["Name"].Value.Equals(documentName) && documentNode.Attributes["Metadata"].Value.Equals(metadataName))
                            {
                                XmlElement folderElement = myXmlDoc.CreateElement("Folder");
                                folderElement.SetAttribute("RelativeUrl",relativeUrl);
                                documentNode.AppendChild(folderElement);
                                myXmlDoc.Save(xmlFilePath);
                            }
                        }
                    }
                }
            }
        }

        public void addDocument(string xmlFilePath,string relativeUrl,string fileName,string metadata)
        {
            addWebTag(xmlFilePath,webUrl);
            addDocumentTag(xmlFilePath,webUrl,documentName,metadataName);
            addFolderTag(xmlFilePath,metadataName,relativeUrl);

            XmlDocument myXmlDoc = new XmlDocument();
            myXmlDoc.Load(xmlFilePath);
            XmlNode rootNode = myXmlDoc.SelectSingleNode("Site");
            XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
            XmlNodeList webNodes = rootNode.ChildNodes;
            foreach (XmlNode webNode in firstLevelNodeList)
            {
                if (webNode.Attributes["Url"].Value.Equals(webUrl))
                {
                    XmlNodeList documentNodes = webNode.ChildNodes;
                    foreach (XmlNode documentNode in documentNodes)
                    {
                        if (documentNode.Attributes["Name"].Value.Equals(documentName) && documentNode.Attributes["Metadata"].Value.Equals(metadataName))
                        {
                            XmlNodeList folderNodes = documentNode.ChildNodes;
                            foreach (XmlNode folderNode in folderNodes)
                            {
                                if (folderNode.Attributes["RelativeUrl"].Value.Equals(relativeUrl))
                                {
                                    XmlElement newElement = myXmlDoc.CreateElement("Item");
                                    newElement.SetAttribute("Name",fileName);
                                    newElement.InnerText = metadata;
                                    folderNode.AppendChild(newElement);
                                }
                            }
                        }
                    }
                }
            }
            myXmlDoc.Save(xmlFilePath);
        }

        public List<string> getDocument(XmlDocument myXmlDoc,string fileName)
        {
            List<string> metadata = new List<string>();

            XmlNode rootNode = myXmlDoc.SelectSingleNode("Site");
            XmlNodeList firstLevelNodeList = rootNode.ChildNodes;

            foreach (XmlNode webNode in firstLevelNodeList)
            {
                if (webNode.Attributes["Url"].Value.Equals(webUrl))
                {
                    XmlNodeList documentNodes = webNode.ChildNodes;
                    foreach (XmlNode documentNode in documentNodes)
                    {
                        if (documentNode.Attributes["Name"].Value.Equals(documentName) && documentNode.Attributes["Metadata"].Value.Equals(metadataName))
                        {
                            XmlNodeList folderNodes = documentNode.ChildNodes;
                            foreach (XmlNode folderNode in folderNodes)
                            {
                                if (folderNode.Attributes["RelativeUrl"].Value.Equals(relativeUrl))
                                {
                                    XmlNodeList itemsNode = folderNode.ChildNodes;
                                    foreach (XmlNode itemNode in itemsNode)
                                    {
                                        if (itemNode.Attributes["Name"].Value.Equals(fileName))
                                        {
                                            metadata.Add(itemNode.InnerText);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return metadata;
        }

        public void DeleteDocument(string path,string metadataName)
        {
            XmlDocument myXmlDoc = new XmlDocument();
            myXmlDoc.Load(path);
            XmlNode rootNode = myXmlDoc.SelectSingleNode("Site");
            XmlNodeList firstLevelNodeList = rootNode.ChildNodes;

            foreach (XmlNode webNode in firstLevelNodeList)
            {
                if (webNode.Attributes["Url"].Value.Equals(webUrl))
                {
                    XmlNodeList documentNodes = webNode.ChildNodes;
                    foreach (XmlNode documentNode in documentNodes)
                    {
                        if (documentNode.Attributes["Name"].Value.Equals(documentName) && documentNode.Attributes["Metadata"].Value.Equals(metadataName))
                        {
                            documentNode.RemoveAll();
                            webNode.RemoveChild(documentNode);
                            myXmlDoc.Save(path);
                            return;
                        }
                    }
                }
            }
        }

        private string importXmlFile()
        {
            string xmlPath = "";
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            dlg.Filter = "(文本文件*.xml)|*.xml";
            dlg.FilterIndex = 1;
            dlg.RestoreDirectory = true;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    xmlPath = dlg.FileName;
                }
                catch (Exception)
                {
                    MessageBox.Show("testdata is wrong");
                }
            }
            return xmlPath;
        }

        private void exportXmlFile()
        {
            string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "SaveData";               //设置标题
            sfd.AddExtension = true;               //是否自动增加所辍名
            sfd.FileName = "storeportal.xml";
            sfd.InitialDirectory = str;  //定义打开的默认文件夹位置
            sfd.Filter = "Text.File(*.xml)|*.xml";
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    string myXMLFilePath = "C:\\Users\\v-trdong\\Desktop\\MyComputers.xml";
                    MetadataXML xml = new MetadataXML();
                    XmlDocument myXmlDoc = new XmlDocument();
                    myXmlDoc.Load(myXMLFilePath);
                    string fileName = sfd.FileName;
                    myXmlDoc.Save(fileName);
                }
                catch (IOException ex)
                {
                    MessageBox.Show(ex.Message,"Simple Editor",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                }
            }
        }
    }







相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念