加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Base operation about XMl file

发布时间:2020-12-15 23:11:49 所属栏目:百科 来源:网络整理
导读: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 addWebT

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:Usersv-trdongDesktopMyComputers.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);
                }
            }
        }
    }



??
??
??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读