Base operation about XMl file

本文介绍了一种在SharePoint 2010到2013迁移过程中操作XML文件的方法,包括添加网站、文档和文件夹标签等基本操作。

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 webUrl, 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 webUrl, String documentName, string metadataName, string relativeUrl, string fileName, string metadata)
        {
            addWebTag(xmlFilePath, webUrl);
            addDocumentTag(xmlFilePath, webUrl, documentName, metadataName);
            addFolderTag(xmlFilePath, webUrl, documentName, 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 webUrl, string documentName, string metadataName, string relativeUrl, 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 webUrl, string documentName, 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);
                }
            }
        }
    }






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值