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);
                }
            }
        }
    }







版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇